Invalid Encoder Readings in Arduino Mega
Posted: Mon Dec 17, 2018 10:07 pm
Hi! I've recently posted in another thread, but I think it's better to start a thread of my own.
I am trying to get readings from my encoder from my Arduino Mega, with the sample code, PacketSerialEncoderPosition, but I am having issues with the Arduino not outputting any values and is invalid.
To start off, I am using a Roboclaw 2x30A, with an Arduino Mega, with the updated Arduino library downloaded directly from Basic Micro's download section, and the firmware of 4.1.27.
When connected to Basic Micro's Motion Studio, auto-tuning works with the encoders are counting in the correct direction (when it is forward, it is counting up, with the speed values that are similar to the max values).
Device Status Page
Device Settings Page
Now here's the problem that I am facing:
When I am using the PacketSerialEncoderPositon sample code in the Arduino Mega, it does not receive the encoder details, it is not outputting any numbers in the serial monitor.
However, when I have the serial monitor open and I then connect the USB from the Roboclaw to my PC (with the Arduino code still running), the values show up.
Is there something that I am doing wrong, and if so, how do I fix it?
Readings with only Arduino Mega Plugged in to PC
Readings with USB plugged in from Roboclaw to PC
Here's the electrical block diagram of how my encoder is connected.
I am trying to get readings from my encoder from my Arduino Mega, with the sample code, PacketSerialEncoderPosition, but I am having issues with the Arduino not outputting any values and is invalid.
To start off, I am using a Roboclaw 2x30A, with an Arduino Mega, with the updated Arduino library downloaded directly from Basic Micro's download section, and the firmware of 4.1.27.
When connected to Basic Micro's Motion Studio, auto-tuning works with the encoders are counting in the correct direction (when it is forward, it is counting up, with the speed values that are similar to the max values).
Device Status Page
Device Settings Page
Now here's the problem that I am facing:
When I am using the PacketSerialEncoderPositon sample code in the Arduino Mega, it does not receive the encoder details, it is not outputting any numbers in the serial monitor.
However, when I have the serial monitor open and I then connect the USB from the Roboclaw to my PC (with the Arduino code still running), the values show up.
Is there something that I am doing wrong, and if so, how do I fix it?
Code: Select all
//See BareMinimum example for a list of library functions
//Includes required to use Roboclaw library
#include <SoftwareSerial.h>
#include "RoboClaw.h"
//See limitations of Arduino SoftwareSerial
SoftwareSerial serial(10,11);
RoboClaw roboclaw(&serial,10000);
#define address 0x80
//Display Encoder and Speed for Motor 1
void displayspeed(void)
{
uint8_t status1,status2;
bool valid1,valid2;
int32_t enc1 = roboclaw.ReadEncM1(address, &status1, &valid1);
int32_t speed1 = roboclaw.ReadSpeedM1(address, &status2, &valid2);
if(valid1){
Serial.print("Encoder1:");
Serial.print(enc1,DEC);
Serial.print(" ");
Serial.print(status1,HEX);
Serial.print(" ");
} else {
Serial.print("Invalid?");
}
if(valid2){
Serial.print("Speed1:");
Serial.print(speed1,DEC);
Serial.print(" ");
}
Serial.println();
}
//This is the first function arduino runs on reset/power up
void setup() {
//Open Serial and roboclaw at 38400bps
Serial.begin(57600);
roboclaw.begin(38400);
Serial.println("Starting...");
}
void loop() {
roboclaw.ForwardM1(address, 50);
long last = millis();
while(millis()-last<5000){
displayspeed();
delay(50);
}
}
Readings with only Arduino Mega Plugged in to PC
Readings with USB plugged in from Roboclaw to PC
Here's the electrical block diagram of how my encoder is connected.