Multi-Mode Packet Serial
Posted: Sun Apr 21, 2019 3:48 pm
List of parts:
Two roboclaw 2x30A in multi-mode packet serial.
Arduino Mega as the MCU.
2 32 RPM Premium Planetary Gear Motors with Encoders.
Using a 12 V 30 A DC power supply in voltage clamp setting.
I'm having trouble getting the multi-mode to work correctly. My arduino code runs with one roboclaw (not in multimode but in packet serial mode) but when I go to tie the tx and rx together the code doesn't work.
Since the Arduino is being power via usb cable I don't suppose I need the 5V and GND attached to the roboclaw from the Arduino.
I saw in another post that the motors i'm using have issues with the encoders so my team and I are hard-coding in time-delays for positioning which is unfortunate but it will suffice.
A brief over view of this code, we are using an x-y lead screw rail with a lidar and separate turntable to try and do 3D scanning of an object. The serial monitor returns lidar distance(z axis), x position(horizontal motor), y position (vertical motor), and phase (turntable position).
This code works for the positioning system(which is M1 and M2 for address)
but when I go to set the multimode on both roboclaws, with baud rate of 38400 on everything and I uncommect the turntable in the void loop the positioning system ceases to move.
Any suggestions?
Two roboclaw 2x30A in multi-mode packet serial.
Arduino Mega as the MCU.
2 32 RPM Premium Planetary Gear Motors with Encoders.
Using a 12 V 30 A DC power supply in voltage clamp setting.
I'm having trouble getting the multi-mode to work correctly. My arduino code runs with one roboclaw (not in multimode but in packet serial mode) but when I go to tie the tx and rx together the code doesn't work.
Since the Arduino is being power via usb cable I don't suppose I need the 5V and GND attached to the roboclaw from the Arduino.
I saw in another post that the motors i'm using have issues with the encoders so my team and I are hard-coding in time-delays for positioning which is unfortunate but it will suffice.
A brief over view of this code, we are using an x-y lead screw rail with a lidar and separate turntable to try and do 3D scanning of an object. The serial monitor returns lidar distance(z axis), x position(horizontal motor), y position (vertical motor), and phase (turntable position).
This code works for the positioning system(which is M1 and M2 for address)
but when I go to set the multimode on both roboclaws, with baud rate of 38400 on everything and I uncommect the turntable in the void loop the positioning system ceases to move.
Any suggestions?
Code: Select all
/* Planar Positioning System with Lidar*/
#include "RoboClaw.h"
#include <Wire.h>
#include <LIDARLite.h>
// Globals
LIDARLite lidarLite;
int cal_cnt = 0;
int dist = 0;
// Roboclaw object
RoboClaw roboclaw(&Serial2,10000); //Connecting TX, RX to roboclaw object
#define address 0x80 //address to roboclaw positioning system
#define address2 0x81 //address to roboclaw turntable
int NUM_ROWS = 25; //Number of rows
int NUM_COLUMNS = 10; //Number of columns
int x_position; //Hold coordinate of x position
int x_position_2;
int y_position; //Hold coordinate of y position
int phase = 0; //Hold coordinate of turntable
int endLoop = 0; //counter to run once
const int buttonPin = 8; //pushbutton pin, connect ground to pin
int buttonState = 0; // either HIGH OR LOW, LOW initially
void setup() {
pinMode(buttonPin, INPUT);
//Open roboclaw serial ports
Serial.begin(38400);
lidarLite.begin(0, true); // Set configuration to default and I2C to 400kHz
lidarLite.configure(0); // Change this number to try out a iternate configurations
Serial3.begin(38400);
roboclaw.begin(38400); //communication with roboclaw, baud rate set the same inside Motion Studio
}
void lidar()
{
dist = lidarLite.distance();
}
void turntable(){
roboclaw.ForwardM1(address2, 127);
delay(2920);
roboclaw.ForwardM1(address2, 0);
roboclaw.ForwardM1(address2, 0);
roboclaw.ForwardM1(address2, 0);
roboclaw.ForwardM1(address2, 0);
phase = phase + 90;
delay(100);
}
/*Loop Algorithm*/
void handlePosition(){
for(y_position = 0; y_position < NUM_COLUMNS ; y_position++){
// Serial.print("HELLO");
if(y_position % 2 == 0){
for(x_position = 0; x_position <= NUM_ROWS; x_position++){
roboclaw.BackwardM2(address, 0);
lidar();
Serial.print(dist);
Serial.print(" ");
Serial.print(x_position);
Serial.print(" ");
Serial.print(y_position);
Serial.print(" ");
Serial.println(phase);
delay(100);
roboclaw.ForwardM1(address,127); //start Motor1 forward at half speed
}
if(x_position == NUM_ROWS){
roboclaw.ForwardM1(address, 0);
//x_position = 0;
}
}
else{
for(x_position_2 = NUM_ROWS; x_position_2 > 0; x_position_2--){
// Serial.print("HI");
roboclaw.BackwardM2(address, 0);
lidar();
Serial.print(dist);
Serial.print(" ");
Serial.print(x_position_2);
Serial.print(" ");
Serial.print(y_position);
Serial.print(" ");
Serial.println(phase);
delay(100);
roboclaw.BackwardM1(address,127); //start Motor1 forward at half speed
}
if(x_position_2 == 0){
//Serial.print("YO");
roboclaw.BackwardM1(address, 0);
//x_position = 0;
}
}
lidar();
Serial.print(dist);
Serial.print(" ");
Serial.print(x_position);
Serial.print(" ");
Serial.print(y_position);
Serial.print(" ");
Serial.println(phase);
delay(100);
roboclaw.BackwardM2(address,127); //start Motor1 forward at full speed
delay(1000);
roboclaw.BackwardM2(address, 0); //stop Motor 1 forward
}
roboclaw.BackwardM1(address, 0);
roboclaw.BackwardM2(address, 0);
}
void loop() {
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH){
while(phase < 360){
handlePosition();
// turntable();
}
}
}