CRITR/motor control gen1

From Lofaro Lab Wiki
Jump to: navigation, search

For our motor control, we used Python's Curses library. We used this because it was an easy implementation in order to implement Tx/Rx serial communication between the Raspberry Pi and the Arduino Pro Mini. There were a few other ways to implement the commands, with pi3d, but Curses allowed us to control the motors and by implementing it through this, we were able to move on and try to control the motors through potentiometers. The downside to using Curses is that it didn't allow for multiple buttons to be read in. Each input given by the user was recorded and holding down the key wasn't read. Pi3d allows for multiple keys to be pressed down simultaneously.

 // pin ID's
 int led = 13;
 int rx = 0;
 int tx = 1;
 // speed
 int pwm1 = 5; //INA on HBRIDGE 1 and 2
 int pwm2 = 9; //INA on HBRIDGE 3 and 4
 // direction
 int pwm_dir1 = 3; //INB on HBRIDGE 1 and 2
 int pwm_dir2 = 6; //INB on HBRIDGE 3 and 4
 // enable EN/IN mode on hbridge
 int pwm_en1 = 2; //PWM on HBRIDGE 1 and 2
 int pwm_en2 = 4; //PWM on HBRIDGE 3 and 4
 
 void setup() {
   // put your setup code here, to run once:
   // the LED was used to show whether we set up the Raspberry Pi to Pro Mini correctly
   //    the LED being on allowed us to see whether or not the pin was set to high or not
   pinMode(led, OUTPUT);
   pinMode(rx, INPUT);
   // we set the initial states of the Rx and Tx pins to low, because we didn't initially
   //    have logic level shifters
   digitalWrite(rx, LOW);
   digitalWrite(tx, LOW);
 
   // Each of the Pro Mini pins were initialized as outputs to enable the mini to speak to the h-bridges
   pinMode(pwm1, OUTPUT);
   pinMode(pwm2, OUTPUT);
   pinMode(pwm_dir1, OUTPUT);
   pinMode(pwm_dir2, OUTPUT);
   pinMode(pwm_en1, OUTPUT);
   pinMode(pwm_en2, OUTPUT);
 
   // the enable was initilized to high to always be in the specific PWM mode
   digitalWrite(pwm_en1, HIGH);
   digitalWrite(pwm_en2, HIGH);
   Serial.begin(9600);
 }
 
 void loop() {
   // put your main code here, to run repeatedly:
   // check to see what the state is on RX (0=LOW, 1=HIGH)
   //   we used this to check to see when the pro mini was in the correct state, because when you first
   //   supply power to the pro mini, the pins need to run through a cycle to enable the pins to be set correctly
   int val = digitalRead(rx);
   digitalWrite(led, val);       //If RX = HIGH, turn LED on, if RX = LOW, turn LED off
   // serial communication sends strings, so a char type was used to store the value
   char input;
   // in order to work properly, we needed to check to see when data was available, to only perform
   //    when data was present
   if (Serial.available()) {
     // read in the input
     input = Serial.read();
     // 0 was our value to stop. to stop, we turned the speed off with a value of LOW
     if (input == '0') {
       digitalWrite(pwm1, LOW);
       digitalWrite(pwm2, LOW);
       // 1 was the direction of forward. all motors were given the direction of forward with a speed enabled to HIGH, meaning on
     } else if (input == '1') {
       digitalWrite(pwm1, HIGH);
       digitalWrite(pwm2, HIGH);
       digitalWrite(pwm_dir1, HIGH);
       digitalWrite(pwm_dir2, HIGH);
       // 2 was the direction of backwards. all motors were given the direction of backwards with speed enabled
     } else if (input == '2') {
       digitalWrite(pwm1, HIGH);
       digitalWrite(pwm2, HIGH);
       digitalWrite(pwm_dir1, LOW);
       digitalWrite(pwm_dir2, LOW);
       // 3 was our direction of left. 1 side of motors was given forward, the other backwards. all being set to speed enabled
       // this setup was to turn in place
     } else if (input == '3') {
       digitalWrite(pwm1, HIGH);
       digitalWrite(pwm2, HIGH);
       digitalWrite(pwm_dir1, LOW);
       digitalWrite(pwm_dir2, HIGH);
       // 4 was our direction of right. the opposide side of motors was given forward, the other backwards. all being set to speed enabled
       // this setup was to turn in place
     } else if (input == '4') {
       digitalWrite(pwm1, HIGH);
       digitalWrite(pwm2, HIGH);
       digitalWrite(pwm_dir1, HIGH);
       digitalWrite(pwm_dir2, LOW);
     }
   }
 }