Setup: UNO+Ada Motor Shield+ Tactic RC Receiver power two dc motor on Magician Chassis
Uno PIN 2: connected to throttle from RC receiver while PIN 13: connected to RC Receiver Steering
DC motor connected to Motor Shield M1 and M4
Uno powered by 9v battery and Motor shield powered from Uno (5V).
Motor shield is sitting on top of Uno (SHield stacking headers. No power Jumper)
The LED on Motor shield is ON
Uno recieves the throttle and steer signal from the RC receiver. However the motors do not run
I have attached the code that I'm using. Specifically the powercar method makes the call to run the motor
Any help in resolving this issues is greatly appreciated
Code: Select all
#include <AFMotor.h>
#define THROTTLE_SIGNAL_IN 0 // INTERRUPT 0 = DIGITAL PIN 2 - use the interrupt number in attachInterrupt
#define THROTTLE_SIGNAL_IN_PIN 2 // INTERRUPT 0 = DIGITAL PIN 2 - use the PIN number in digitalRead
#define NEUTRAL_THROTTLE 1500 // this is the duration in microseconds of neutral throttle on an electric RC Car
#define NEUTRAL_DIRECTION 1500 // this is the duration in microseconds of neutral throttle on an electric RC Car
#define DIRECTION_SIGNAL_IN 1 // INTERRUPT 1 = DIGITAL PIN 3 - use the interrupt number in attachInterrupt
#define DIRECTION_SIGNAL_IN_PIN 3 // INTERRUPT 1 = DIGITAL PIN 3 - use the PIN number in digitalRead
#define NEUTRAL 0 // this is the duration in microseconds of neutral on an electric RC Car
//Throttle
volatile int g_i_SpeedIn = NEUTRAL; // volatile, we set this in the Interrupt and read it in loop so it must be declared volatile
volatile unsigned long ulStartPeriodThrottle = 0; // set in the interrupt
volatile boolean bNewThrottleSignal = false; // set in the interrupt and read in the loop
//Direction
volatile int g_i_SteerIn = NEUTRAL; // volatile, we set this in the Interrupt and read it in loop so it must be declared volatile
volatile int g_i_LeftMotor=NEUTRAL;
volatile int g_i_RightMotor=NEUTRAL;
int lowThrottle = 1100;
int highThrottle = 1900;
int lowDirection = 1100;
int highDirection = 1900;
int speedLimit =255;
int directionLimit=255;
int deadband = 50; // sets the total deadband - this number is divided by 2 to get the deadband for each direction. Higher value will yield larger neutral band.
int deadband_high = deadband/2; // sets deadband_high to be half of deadband (ie. 10/2 = 5)
int deadband_low = -deadband_high; // sets deadband_low to be negative half of deadband (ie. 5 * -1 = -5)
int g_i_ppm_steering = 13; //Steering RC in
AF_DCMotor adaleftmotor(1, MOTOR12_1KHZ); // create motor #1, 1KHz pwm
AF_DCMotor adarightmotor(4, MOTOR12_1KHZ); // create motor #4, 1KHz pwm
void setup() {
attachInterrupt(THROTTLE_SIGNAL_IN,calcThrottleInput,CHANGE); //Monitor interrupt in Pin 2 Throttle
pinMode(g_i_ppm_steering, INPUT); //Steering in Pin 13 from RC
Serial.begin(9600);
}
void loop(){
if(bNewThrottleSignal) bNewThrottleSignal = false;
calcSteerInput(); //Pulse in calculation of steering
printSpeedDirection();
PowerCar(); //Compute left and righ motor PWM value and call Ada motor controller functions
}
//Interrupt triggered computation of throttle from RC control with failsafe on Throttle
void calcThrottleInput(){
int l_i_ThrottleIn ;
if(digitalRead(THROTTLE_SIGNAL_IN_PIN) == HIGH) {
ulStartPeriodThrottle = micros();
}
else {
if(ulStartPeriodThrottle && (bNewThrottleSignal == false)) {
l_i_ThrottleIn = (int)(micros() - ulStartPeriodThrottle);
g_i_SpeedIn = map(l_i_ThrottleIn,lowThrottle,highThrottle,-speedLimit,speedLimit);
g_i_SpeedIn = constrain(g_i_SpeedIn , -speedLimit,speedLimit);
if (g_i_SpeedIn >= deadband_low && g_i_SpeedIn <= deadband_high) g_i_SpeedIn = NEUTRAL;
ulStartPeriodThrottle = 0;
bNewThrottleSignal = true;
}
}
}
//Monitor Pin 13 for steering . No failsafe
void calcSteerInput(){
int l_i_DirectionIn ;
l_i_DirectionIn = pulseIn(g_i_ppm_steering, HIGH, 20000); // read pulse from RC channel 1
g_i_SteerIn = map(l_i_DirectionIn ,highDirection,lowDirection,-directionLimit,directionLimit);
g_i_SteerIn = constrain(g_i_SteerIn, -directionLimit,directionLimit);
if (g_i_SteerIn >= deadband_low && g_i_SteerIn <= deadband_high) g_i_SteerIn = NEUTRAL;
}
void PowerCar(){
float l_f_lm, l_f_rm,l_f_steerfactor;
l_f_lm =1.0;
l_f_rm =1.0;
if (g_i_SteerIn != NEUTRAL) {
l_f_steerfactor =((2 * (float)g_i_SteerIn)/((float)directionLimit - (float)NEUTRAL));
(g_i_SteerIn > NEUTRAL) ? l_f_rm -= l_f_steerfactor : l_f_lm += l_f_steerfactor;
}
l_f_lm *= (float) g_i_SpeedIn ;
l_f_rm *= (float) g_i_SpeedIn ;
g_i_LeftMotor = constrain((int) l_f_lm, -speedLimit,speedLimit); //Safety
g_i_RightMotor = constrain((int) l_f_rm, -speedLimit,speedLimit); //Safety
//Code to use adafruit motor controller
if (g_i_LeftMotor > 0) {
adaleftmotor.run(FORWARD);
Serial.println("Left Forward");
} else {
adaleftmotor.run(BACKWARD);
Serial.println("Left Backward");
}
adaleftmotor.setSpeed(abs(g_i_LeftMotor)); // set the speed
if (g_i_RightMotor > 0) {
adarightmotor.run(FORWARD);
Serial.println("RightForward");
} else {
adarightmotor.run(BACKWARD);
Serial.println("Right Backward");
}
adaleftmotor.setSpeed(abs(g_i_RightMotor)); // set the speed
}
void printSpeedDirection(){
Serial.print(" Speed: ");
Serial.print(g_i_SpeedIn );
Serial.print(" Steer: ");
Serial.print(g_i_SteerIn);
Serial.print(" Left: ");
Serial.print(g_i_LeftMotor);
Serial.print(" Right: ");
Serial.println(g_i_RightMotor);
}