Guest User

Untitled

a guest
Feb 19th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1. //------------------------------------------------------------------------------
  2. // CANAKIT h-bridge test #2
  3. // 20111022 dan@marginallyclever.com
  4. //------------------------------------------------------------------------------
  5.  
  6. #define IN1  (40)  // direction pins for motor 1
  7. #define IN2  (41)
  8. #define IN3  (42)  // direction pins for motor 2
  9. #define IN4  (43)
  10. #define IN5  (44)  // direction pins for motor 3
  11. #define IN6  (45)
  12. #define IN7  (46)  // direction pins for motor 4
  13. #define IN8  (47)
  14. #define IN9  (48)  // direction pins for motor 5
  15. #define IN10 (49)
  16. #define IN11 (50)  // direction pins for motor 6
  17. #define IN12 (51)
  18.  
  19.  
  20. // these values MUST remain sequential.
  21. #define PWM1 ( 8)  // PWM speed control on motor 1
  22. #define PWM2 ( 9)  // PWM speed control on motor 2
  23. #define PWM3 (10)  // PWM speed control on motor 3
  24. #define PWM4 (11)  // PWM speed control on motor 4
  25. #define PWM5 (12)  // PWM speed control on motor 5
  26. #define PWM6 (13)  // PWM speed control on motor 6
  27.  
  28. // Internally we treat speeds as 0...1
  29. // analogWrite(PWM*) goes from 0 to 255.
  30. #define SERVO_SCALE (255)
  31.  
  32.  
  33. // these values MUST remain sequential.
  34. #define SENSOR1 (0)  // the potentiometer for motor 1
  35. #define SENSOR2 (1)  // the potentiometer for motor 2
  36. #define SENSOR3 (2)  // the potentiometer for motor 3
  37. #define SENSOR4 (3)  // the potentiometer for motor 4
  38. #define SENSOR5 (4)  // the potentiometer for motor 5
  39. #define SENSOR6 (5)  // the potentiometer for motor 6
  40.  
  41. //------------------------------------------------------------------------------
  42. // pwm - PWM[N] motor defined above
  43. //
  44. // returns a value [0...1), 0 being fully retracted and 1 is fully extended.
  45. //------------------------------------------------------------------------------
  46. float ReadActuatorPos(int pwm) {
  47.   int sensor;
  48.   // sensor - SENSOR[N] defines above
  49.   switch(pwm) {
  50.   case PWM1:    sensor=SENSOR1;    break;
  51.   case PWM2:    sensor=SENSOR2;    break;
  52.   case PWM3:    sensor=SENSOR3;    break;
  53.   case PWM4:    sensor=SENSOR4;    break;
  54.   case PWM5:    sensor=SENSOR5;    break;
  55.   case PWM6:    sensor=SENSOR6;    break;
  56.   }
  57.  
  58.   return (float)analogRead(sensor) / 1024.0f;
  59. }
  60.  
  61.  
  62. //------------------------------------------------------------------------------
  63. // pwm - see PWM[N] defines above
  64. // newspeed - [0....1]
  65. //------------------------------------------------------------------------------
  66. void SetActuatorSpeed(int pwm,float newspeed) {
  67.   int pin1, pin2, mode1, mode2;  
  68.  
  69.   if( newspeed > 0 ) {
  70.     mode1=HIGH;
  71.     mode2=LOW;
  72.     Serial.print("E");
  73.   } else if( newspeed < 0 ) {
  74.     mode1=LOW;
  75.     mode2=HIGH;
  76.     newspeed=-newspeed;
  77.     Serial.print("R");
  78.   } else {
  79.     // mode1=HIGH, mode2=HIGH is the same as mode1=LOW, mode2=LOW
  80.     mode1=HIGH;
  81.     mode2=HIGH;
  82.     Serial.print("L");
  83.   }
  84.  
  85.   switch(pwm) {
  86.   case PWM1:    pin1=IN1;    pin2=IN2;    Serial.print("pin1=IN1  pin2=IN2  ");  break;
  87.   case PWM2:    pin1=IN3;    pin2=IN4;    Serial.print("pin1=IN3  pin2=IN4  ");  break;
  88.   case PWM3:    pin1=IN5;    pin2=IN6;    Serial.print("pin1=IN5  pin2=IN6  ");  break;
  89.   case PWM4:    pin1=IN7;    pin2=IN8;    Serial.print("pin1=IN7  pin2=IN8  ");  break;
  90.   case PWM5:    pin1=IN9;    pin2=IN10;   Serial.print("pin1=IN9  pin2=IN10 ");  break;
  91.   case PWM6:    pin1=IN11;   pin2=IN12;   Serial.print("pin1=IN11 pin2=IN12 ");  break;
  92.   default: return;
  93.   }
  94.  
  95.   Serial.print(pwm-PWM1+1);
  96.   Serial.print("=");
  97. //  Serial.print(newspeed);
  98. //  Serial.print("=");
  99.   Serial.print(mode1);
  100.   Serial.print("/");
  101.   Serial.print(mode2);
  102.   Serial.print("\t");
  103.  
  104.   // set direction
  105.   digitalWrite(pin1,mode1);
  106.   digitalWrite(pin2,mode2);
  107.   // set speed
  108.   analogWrite(pwm,newspeed*SERVO_SCALE);
  109. }
  110.  
  111.  
  112. //------------------------------------------------------------------------------
  113. void setup() {
  114.   Serial.begin(57600);  // open serial link for debug output
  115.  
  116.   // prepare the pins that control the motor(s) movement modes
  117.   pinMode(IN1,OUTPUT);
  118.   pinMode(IN2,OUTPUT);
  119.   pinMode(IN3,OUTPUT);
  120.   pinMode(IN4,OUTPUT);
  121.   pinMode(IN5,OUTPUT);
  122.   pinMode(IN6,OUTPUT);
  123.   pinMode(IN7,OUTPUT);
  124.   pinMode(IN8,OUTPUT);
  125.   pinMode(IN9,OUTPUT);
  126.   pinMode(IN10,OUTPUT);
  127.   pinMode(IN11,OUTPUT);
  128.   pinMode(IN12,OUTPUT);
  129. }
  130.  
  131.  
  132. //------------------------------------------------------------------------------
  133. void loop() {
  134.   // @TODO: Remove this resets all the acutators to their retracted position.
  135.   // @TODO: it is temporary and should not be left in.
  136.   int j,i;
  137.   float f;
  138. //*
  139.   do {
  140.     j=0;
  141.     for(i=0;i<6;++i) {
  142.       f=ReadActuatorPos(PWM1+i);
  143.       if(f>0.51) SetActuatorSpeed(PWM1+i,-1);
  144.       else if(f<0.49) SetActuatorSpeed(PWM1+i,1);
  145.       else {
  146.         SetActuatorSpeed(PWM1+i,0);
  147.         ++j;
  148.       }
  149.     }
  150.   } while(j<6);
  151. /*/
  152.   for(i=0;i<6;++i) {
  153.     while(ReadActuatorPos(PWM1+i)>0.5) SetActuatorSpeed(PWM1+i,-1);
  154.     while(ReadActuatorPos(PWM1+i)<0.5) SetActuatorSpeed(PWM1+i,1);
  155.     SetActuatorSpeed(PWM1+i,0);
  156.   }
  157. //*/
  158. }
Add Comment
Please, Sign In to add comment