Hi there. I'm working at using a Monster Motor sheild + R/C receiver to get my son's old RC car to move again. It's a pretty big car with a big brushed motor.
I changed the steering servo to a regular R/C steering servo and have that directly plugged into the Receiver. I'm using a 3S lipo battery wired to the single Monster Motor driver and through a 5V buck converter to the Arduino.
I used the code from here for an example on how to do this and changed some stuff around to make it work for a my motor driver and a single motor instead of two.
My code currently looks like this.
/**
* One Channel Receiver
* Author: Shawn Hymel (SparkFun Electronics)
* Date: Aug 17, 2017
*
* Connect a TB6612FNG and RC (PWM) receiver to the Arduino.
* Only works 1 channel for forward and back drive.
*
* This code is beerware; if you see me (or any other SparkFun
* employee) at the local, and you've found our code helpful,
* please buy us a round!
* Distributed as-is; no warranty is given.
*/
// This sketch was modded by Jonathan 2021 January to work for Black Hummer.
// Controller pins
const int CH_2_PIN = 2;
// Motor driver pins
const int STBY_PIN = A0;
const int AIN1_PIN = 7;
const int AIN2_PIN = 8;
const int APWM_PIN = 5;
#define CURRENT_SEN_1 A2
#define CS_THRESHOLD 200 // Definition of safety current (Check: "1.3 Monster Shield Example")
// Parameters
const int deadzone = 20; // Anything between -20 and 20 is stop
void setup() {
Serial.begin(9600);
// Configure pins
pinMode(STBY_PIN, OUTPUT);
pinMode(AIN1_PIN, OUTPUT);
pinMode(AIN2_PIN, OUTPUT);
pinMode(APWM_PIN, OUTPUT);
pinMode(CURRENT_SEN_1, INPUT);
// Enable motor driver
digitalWrite(STBY_PIN, HIGH);
}
void loop() {
// Read pulse width from receiver
int ch_2 = pulseIn(CH_2_PIN, HIGH, 25000);
// Convert to PWM value (-255 to 255)
ch_2 = pulseToPWM(ch_2);
// Drive motor
drive(ch_2, ch_2);
delay(5);
}
// Positive for forward, negative for reverse
void drive(int speed_a, int speed_b) {
// Limit speed between -255 and 255
speed_a = constrain(speed_a, -255, 255);
speed_b = constrain(speed_b, -255, 255);
// Set direction for motor A
if ( speed_a == 0 ) {
digitalWrite(AIN1_PIN, LOW);
digitalWrite(AIN2_PIN, LOW);
} else if ( speed_a > 0 ) {
digitalWrite(AIN1_PIN, HIGH);
digitalWrite(AIN2_PIN, LOW);
} else {
digitalWrite(AIN1_PIN, LOW);
digitalWrite(AIN2_PIN, HIGH);
}
// Set speed
analogWrite(APWM_PIN, abs(speed_a));
}
// Convert RC pulse value to motor PWM value
int pulseToPWM(int pulse) {
// If we're receiving numbers, convert them to motor PWM
if ( pulse > 995 ) {
pulse = map(pulse, 996, 2000, -500, 500);
pulse = constrain(pulse, -255, 255);
} else {
pulse = 0;
}
// Anything in deadzone should stop the motor
if ( abs(pulse) <= deadzone ) {
pulse = 0;
}
return pulse;
}
Everything seems to work perfectly on the bench with tires off the ground. Speed control works perfectly forward and backwards. The problem is when I put it on the ground it seems to choke. When I move everything slowly things work well, but as soon as I punch it a bit to make it accelerate fast it stops and stutters. Sometimes when this happens, I can't revers motor until I gently go forward for a second then everything works normally as long as I don't make it work too hard. This is only one or two second bursts so I don't suspect overheating.
My guess is that it has something to do with current sense, but I've increased that by a lot to see if it makes a difference, I have not seen anything. Is there something missing from my code?