ソースを参照

added maxSpeed on A0

Etienne Landon 6 年 前
コミット
0f0a138701
1 ファイル変更25 行追加33 行削除
  1. 25 33
      CG_MarioKart.ino

+ 25 - 33
CG_MarioKart.ino

@@ -25,6 +25,11 @@ int dirs_pin[2] = {4, 7};
 
 int threshold = 30 ;
 
+const int maxSpeed_pin = A0;
+const int driftCorrection_pin = A1 ;
+int maxSpeed = 0;
+float driftCorrection = 0 ;
+
 
 int middleThreshold (int RCvalue, int threshold) {  // 
 
@@ -57,6 +62,18 @@ Serial.begin(9600); // Pour a bowl of Serial (for debugging)
 
 void loop() {
 
+  //Read and scale analog inputs
+  maxSpeed = map (analogRead(maxSpeed_pin), 0, 1024, 0, 255);
+  driftCorrection = analogRead(driftCorrection_pin) ;
+  driftCorrection -= 512;
+  driftCorrection /= 8 ;
+
+  Serial.print("maxSpeed:"); //Serial debugging stuff
+  Serial.println(maxSpeed);
+  Serial.print("correction:"); //Serial debugging stuff
+  Serial.println(driftCorrection);
+  
+
   //Read RC values and give wide center point
   ch1 = pulseIn(ch1_pin, HIGH, 25000); // Read the pulse width of  
   ch2 = pulseIn(ch2_pin, HIGH, 25000); // each channel
@@ -66,8 +83,6 @@ void loop() {
   //Refresh and scale raw move values
   move = map(ch2, 1000,2000, -255, 255); //center over zero
   move = constrain(move, -255, 255);
-//  turn = map(ch1,1000,2000,-255,255);
-//  turn = constrain(turn, -255, 255);
   turn = map(ch1,1000,2000,-255,255);
  
 
@@ -79,16 +94,17 @@ void loop() {
   
   //calculate each motor speed
   int speeds[2] = {0, 0} ;
-//  int dirs[2] = {0, 0} ;
   
   int Dir = move>0 ? 1 : 0;
-  move = abs(move) ;
+  move = min( abs(move), maxSpeed) ;
   turn = constrain(turn, -move, move);
+  
   if (turn>0) { speeds[0] = move; speeds[1] = move - turn  ; }
-  if (turn<=0) { speeds[0] = move + turn ; speeds[1] = move   ; 
-    }
-//  speeds[1] = move  ; 
-//  speeds[0] = move   ;
+  if (turn<=0) { speeds[0] = move + turn ; speeds[1] = move   ;  }
+
+//  if (driftCorrection>0) { speeds[0] -= driftCorrection*; speeds[1] = move - turn  ; }
+//  if (driftCorrection<=0) { speeds[0] = move + turn ; speeds[1] = move   ;  }
+//  
   Serial.print("speeds: "); //Serial debugging stuff
   Serial.print(speeds[0]);
   Serial.print(" - "); 
@@ -96,38 +112,14 @@ void loop() {
   
   //apply to driver
   for(int i =0; i<2; i++) {
-//    dirs[i] = speeds[i] > 0 ? 1 : 0 ;
     speeds[i] = abs( speeds[i]);
+    
     digitalWrite(dirs_pin[i], Dir) ;
     analogWrite(speeds_pin[i], speeds[i]) ;
     
   }
 
-//  analogWrite (
-// 
-//  
-//  /*This is where we do some mixing, by subtracting our "turn" 
-//  variable from the appropriate motor's speed we can execute
-//  a turn in either direction*/
-//  if(turn>=0){analogWrite(pwm_b, move-turn); analogWrite(pwm_a, move);};
-//  if(turn<0){turn=abs(turn); analogWrite(pwm_a, move-turn); analogWrite(pwm_b, move);};
-////  analogWrite(pwm_b, move); analogWrite(pwm_a, move);
-  
-  
-  
-  Serial.print("A: "); //Serial debugging stuff
-  Serial.print(speeds[0]); 
-  Serial.print(" - "); 
-//  Serial.println(dirs[0]);
-  
-  Serial.print("B: "); //Serial debugging stuff
-  Serial.print(speeds[1]); 
-  Serial.print(" - "); 
-//  Serial.println(dirs[1]);
-  
-  Serial.println(); //Serial debugging stuff
   Serial.println();
-//  Serial.println();
 
 }