CRITR/motor control gen2

From Lofaro Lab Wiki
Jump to: navigation, search

For our generation two, we made an attempt to user potentiometers to allow the use of a joystick to manipulate the motor control. Below is the code we have ready to test. We weren't able to test this code, due to time limitations and because of discrepancies in the formatting of the serial communication between the server and client code and the motor control code. Different people made each component and made amendments that we didn't settle on. The code is just an idea of what we would have implemented. The given ranges for differing speeds might need to be adjusted, due to the physical limitations induced by our design of the joystick. To combat this, you would need to choose a better joystick design that allowed a bigger range of motion for the potentiometers or to redefine the ranges for variable speeds in the motor control code.

 //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);
   // the delay was used to allow the setup of the components, before processing anything
   delay(3000);                  // waits for 3 seconds
 }
 
 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();
 
     //potentiometer 1  (first part of input)
     //down - stop - up
     // if the value of the potentiometer has a reading from 0 to 500, set the directions of 1 side to backwards
     //    within the backwards and forwards, there are divisions to allow 3 different speeds
     if (input > 0) {
       digitalWrite(pwm_dir1, LOW);
       analogWrite(pwm1, 85);
     } else if (input > 170) {
       digitalWrite(pwm_dir1, LOW);
       analogWrite(pwm1, 170);
     } else if (input > 340) {
       digitalWrite(pwm_dir1, LOW);
       analogWrite(pwm1, 255);
       // if the value is between 500 and 524, a cushion, to allow no movement, speed is set to 0
     } else if (input > 500 && input < 524) {
       analogWrite(pwm1, 0);
       // if the value is between 524 and 1023, turn the side to high with variable speeds
     } else if (input > 524) {
       digitalWrite(pwm_dir1, HIGH);
       analogWrite(pwm1, 85);
     } else if (input > 682) {
       digitalWrite(pwm_dir1, HIGH);
       analogWrite(pwm1, 170);
     } else if (input > 852) {
       digitalWrite(pwm_dir1, HIGH);
       analogWrite(pwm1, 255);
     }
 
     //potentiometer 2
     //down - stop - up
     if (input > 0) {
       digitalWrite(pwm_dir2, LOW);
       analogWrite(pwm2, 85);
     } else if (input > 170) {
       digitalWrite(pwm_dir2, LOW);
       analogWrite(pwm2, 170);
     } else if (input > 340) {
       digitalWrite(pwm_dir2, LOW);
       analogWrite(pwm2, 255);
     } else if (input > 500 && input < 524) {
       digitalWrite(pwm2, HIGH);
       analogWrite(pwm2, 0);
     } else if (input > 524) {
       digitalWrite(pwm_dir2, HIGH);
       analogWrite(pwm2, 85);
     } else if (input > 682) {
       digitalWrite(pwm_dir2, HIGH);
       analogWrite(pwm2, 170);
     } else if (input > 852) {
       digitalWrite(pwm_dir2, HIGH);
       analogWrite(pwm2, 255);
     }
   }
 }