I am controlling a single motorhttps://www.gobilda.com/5202-series-yellow-jacket-planetary-gear-motor-50-9-1-ratio-117-rpm-3-3-5v-encoder/
I am using a High-Power Simple Motor Controller G2 18v25
I am using a Nano. The issue I am having is the encoder is dropping steps. I can not control the position accurately.
I think the issue is coming from the software.serial sending the motor speed and direction. When I use just the encoder code with serial monitor the encoder steps are reading accurately.
Is this an issue where I need a different motor controller.
#include <SoftwareSerial.h>
#include <Encoder.h>
#define rxPin 3 // pin 3 connects to smcSerial TX (not used in this example)
#define txPin 4 // pin 4 connects to smcSerial RX
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(5, 6);
SoftwareSerial smcSerial = SoftwareSerial(rxPin, txPin);
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
smcSerial.write(0x83);
}
// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
if (speed < 0)
{
smcSerial.write(0x86); // motor reverse command
speed = -speed; // make speed positive
}
else
{
smcSerial.write(0x85); // motor forward command
}
smcSerial.write(speed & 0x1F);
smcSerial.write(speed >> 5 & 0x7F);
}
void setup()
{
// Initialize software serial object with baud rate of 19.2 kbps.
smcSerial.begin(19200);
// The Simple Motor Controller must be running for at least 1 ms
// before we try to send serial data, so we delay here for 5 ms.
delay(5);
// If the Simple Motor Controller has automatic baud detection
// enabled, we first need to send it the byte 0xAA (170 in decimal)
// so that it can learn the baud rate.
smcSerial.write(0xAA);
// Next we need to send the Exit Safe Start command, which
// clears the safe-start violation and lets the motor run.
exitSafeStart();
}
long oldPosition = -999;
int motSp = (0);
long setPosition =2850;
void loop()
{
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
}
if (newPosition > setPosition-50 && newPosition < setPosition+50){
motSp = 0;
}
else if ( newPosition < setPosition){
motSp = 1000;
}
else {
motSp = -1500;
}
setMotorSpeed(motSp); // full-speed forward
}