Hello!
I am working on a project where I need to control a motor with an Encoder. I bought the motor and the controller from Pololu, while the Arduino is Elegoo (The specific parts are at the bottom of the post). My first issue comes around the powering the controller, I connect it to a voltage generator, but when picking the voltage suggested on its buy page (3-4.9V) it doesn’t work, but it works with higher voltages (6V). Then I connect the motor to a 12V and its encoder to the 5V pin on the Arduino board. The following script is capable of control the motor in both directions, however it doesn’t stop it when I’d like to:
#include <Motoron.h>
#include <SoftwareSerial.h>
#define ENCA 2
#define ENCB 3
int pos = 0;
int desired_pos = 0;
int speed;
bool isStopped = false; // New flag to check if motor is stopped
SoftwareSerial mySerial(10, 11); // RX, TX
#define mcSerial mySerial // Use SoftwareSerial for the Motoron controller
MotoronSerial mc;
const uint32_t assignBaudRate = 115200;
static_assert(assignBaudRate >= MOTORON_MIN_BAUD_RATE &&
assignBaudRate <= MOTORON_MAX_BAUD_RATE, "Invalid baud rate.");
bool initializeMotoren() {
mc.reinitialize();
mc.disableCrc();
mc.clearResetFlag();
// Verify initialization
uint16_t errors = mc.getErrorMask();
return (errors == 0);
}
void setup() {
// Wait for a moment to ensure stable power and connections
delay(1000);
mcSerial.begin(assignBaudRate); // Start communication with motor controller
Serial.begin(assignBaudRate);
pinMode(ENCA, INPUT);
pinMode(ENCB, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCA), readEncoder, RISING);
// Wait for the initialization command
while (true) {
char command = Serial.read();
if (command == 'I') {
Serial.println("Initialization command received");
break; // Exit the loop and proceed with the setup
}
}
mcSerial.setTimeout(200); //Can be changed to get better values for pos
mc.setPort(&mcSerial);
// Initialize the Motoron
initializeMotoren();
// Configure the motor (motor 1 only)
mc.setMaxAcceleration(1, 0);
mc.setMaxDeceleration(1, 0);
Serial.println("Setup complete");
}
// Declare the processCommand function
void processCommand(String cmd) {
if (cmd == "F"){
mc.setSpeed(1, 200); // Set motor 1 speed to 400
Serial.println("Motor set to forward");
} else if (cmd == "R") {
mc.setSpeed(1, -200); // Set motor 1 speed to reverse
Serial.println("Motor set to reverse");
}
else if (cmd == "Reset") {
pos = 0;
isStopped = false; // Reset the stopped flag when position is reset
}
else if (cmd == "S") {
mc.setSpeed(1, 0); // Stop motor 1
Serial.println("Motor set to stop");
}
else {
Serial.println("Unknown command");
}
}
void loop() {
String command = ""; // Use String to hold multiple characters
while (Serial.available() > 0) {
char incomingChar = Serial.read(); // Read a single character
if (incomingChar == '\n') { // Check for newline (end of input)
processCommand(command); // Process the complete command
command = ""; // Clear the command for the next input
} else {
command += incomingChar; // Append the character to the command
}
}
command = "";
// Stop the motor once pos reaches 300
if (pos == 500){
mc.setSpeedNow(1, 0);
Serial.println("Done");
}
Serial.println(mc.getCurrentSpeed(1));
delay(3);
// Serial.println(pos);
}
// Interrupt service routine for reading the encoder
void readEncoder() {
int b = digitalRead(ENCB); // Read the state of ENCB pin
if (b > 0) {
pos++; // Increment position if ENCB is HIGH
} else {
pos--; // Decrement position if ENCB is LOW
}
}
The main point is here:
// Stop the motor once pos reaches 300
if (pos == 500){
mc.setSpeedNow(1, 0);
Serial.println("Done");
}
When ‘pos’ reaches 300 it does print “Done”, but the motor doesn’t stop. What am I doing wrong?
I tried getting the speed the controller is trying to apply to the motor at each time with
Serial.println(mc.getCurrentSpeed(1));
But it goes quite crazy giving random values, when the motor is stopped sometimes it prints non-zero values. When the motor is turning it gives for the most pretty good values, with some random stuff here and there (1 out of 5 prints is random).
Parts:
Motor - 380:1 Micro Metal Gearmotor HPCB 12V with 12 CPR Encoder, Back Connector
Controller - Motoron M1U550 Single Serial Motor Controller (Header Pins Soldered)
Arduino - Elegoo Nano
Last, the MySerial software isn’t what is giving issues, I tried connecting the Arduino to the RX and TX pins but it didn’t do anything different.
Can someone help?
Thank you
2 posts - 2 participants