We have chosen to use a monster moto motor driver for Mr. Bit. This driver is an Arduino shield format and the control of it is direct from a seeeduino. We wanted to test the motor driver with a simple keyboard input to drive the motors backward and forward. Firstly we had to solder the connector headers, we decided to swap the connectors for stackable ones so, if needed, the shield can take another shield and to simplify any other connections we need to make to Arduino later.
Once soldered and on the seeeduino we have used crocodile clips to wire up the driver to test it using a battery pack and some mini metalgear motors. The seeeduino needs a sketch to control the motors.
The code is simple, it’s a slight change to the example from on the monster moto shield sparkfun webpage. Firstly we set up the pins that will be used to control the drivers, pins are used to either turn on clockwise or anticlockwise direction for the two motor outputs. Pulse width modulation (PWM) is used to alter the speed of each motor. There is also a current sense in which reads the current of each motor (might be useful to protect the motors).
The following code simply uses the serial monitor to read in a key pressed on the laptop and then drive the motors depending on the character pressed: “F” will drive motors forward, “B” backward and “S” will Stop. If this works as expected we will use similar commands into the arduino from the Pi.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
#define BRAKEVCC 0 #define CW 1 #define CCW 2 #define BRAKEGND 3 #define CS_THRESHOLD 100 /* VNH2SP30 pin definitions xxx[0] controls '1' outputs xxx[1] controls '2' outputs */ int inApin[2] = {7, 4}; // INA: Clockwise input int inBpin[2] = {8, 9}; // INB: Counter-clockwise input int pwmpin[2] = {5, 6}; // PWM input int cspin[2] = {2, 3}; // CS: Current sense ANALOG input int enpin[2] = {0, 1}; // EN: Status of switches output (Analog pin) int statpin = 13; char incomingByte; void setup() { Serial.begin(9600); pinMode(statpin, OUTPUT); // Initialize digital pins as outputs for (int i=0; i<2; i++) { pinMode(inApin[i], OUTPUT); pinMode(inBpin[i], OUTPUT); pinMode(pwmpin[i], OUTPUT); } // Initialize braked for (int i=0; i<2; i++) { digitalWrite(inApin[i], LOW); digitalWrite(inBpin[i], LOW); } // motorGo(0, CW, 1023); // motorGo(1, CCW, 1023); } void loop() { if (Serial.available() > 0) { incomingByte = Serial.read(); if(incomingByte == 'F') { motorGo(0, CW, 1023); motorGo(1, CCW, 1023); Serial.println("Moving Forwards"); } if(incomingByte == 'B') { motorGo(0, CCW, 1023); motorGo(1, CW, 1023); Serial.println("Moving Backwards"); } if(incomingByte == 'S') { motorOff(0); motorOff(1); Serial.println("Stopped"); } } delay(10); if ((analogRead(cspin[0]) < CS_THRESHOLD) && (analogRead(cspin[1]) < CS_THRESHOLD)) digitalWrite(statpin, HIGH); } void motorOff(int motor) { // Initialize braked for (int i=0; i<2; i++) { digitalWrite(inApin[i], LOW); digitalWrite(inBpin[i], LOW); } analogWrite(pwmpin[motor], 0); } void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm) { if (motor <= 1) { if (direct <=4) { // Set inA[motor] if (direct <=1) digitalWrite(inApin[motor], HIGH); else digitalWrite(inApin[motor], LOW); // Set inB[motor] if ((direct==0)||(direct==2)) digitalWrite(inBpin[motor], HIGH); else digitalWrite(inBpin[motor], LOW); analogWrite(pwmpin[motor], pwm); } } } |
Here’s a video of the motors in action:
We’re very glad that the motor drivers are working! We’re still not sure about our overall approach through, the motor driver is actually the Arduino and the competition states the core system needs to be the Pi. Just how much work the Pi does vs the Arduino is yet to be seen – the shield itself requires two 5v PWM connections and since the Pi has only one PWM GPIO with max voltage of 3.3v we see no other option right now other than the Arduino.