New user - Greenpower Car

General discussion of using Roboclaw motor controllers
Post Reply
marcopolo
Posts: 2
Joined: Tue Oct 20, 2020 3:32 pm
New user - Greenpower Car

Post by marcopolo »

Hi. I am not the worlds best programmer but using a Teensy 3.5 to control the 60A basic micro. I need to control the motor speed, set different acceleration and deccel settings and limit the current on the fly.

I also need to get back the current volts and temp from the controller ....Sadly nothing except the forward command seems to work. Can anyone help.

So far I have this and just dont get why it does not compile in several places. Nor why it wont read any data back . Nor why I cannot control the acceleration.

Let myself in for this as a charitable project thinking it would just drop out!

#include "RoboClaw.h"
#include <SoftwareSerial.h>

RoboClaw roboclaw = RoboClaw(&Serial5, 10000);
uint16_t address = 0x80;
uint8_t status;
bool valid;

void setup() {
roboclaw.begin(38400);
Serial.begin(9600);
}




void loop() {

uint16_t currentOne;
uint16_t currentTwo;

roboclaw.SetM1DefaultAccel(0x80,2000 );
roboclaw.ForwardM1(0x80,00);


uint32_t Speed = roboclaw.ReadSpeedM1(address);
uint16_t Temp = roboclaw.ReadTemp(address);
uint16_t Volts = roboclaw.ReadMainBatteryVoltage(address);



bool gotIt = roboclaw.ReadCurrents(address, currentOne, currentTwo);
if(gotIt)
{
Serial.print("Hot diggity damn, we got the current data...");
Serial.print("Motor 1 is sucking up ");
Serial.print(currentFromMotorOne);
Serial.println(" amp, or maybe that's milliamps...");
Serial.print("Motor 2 is sucking up ");
Serial.print(currentFromMotorTwo);
Serial.println(" amp, or maybe that's milliamps...");
}

uint32_t speed1 = roboclaw.ReadSpeedM1(address,&status,&valid);
Serial.print("Speed1:");
Serial.print(speed1,DEC);
Serial.print(" ");
Serial.print ("Speed");
Serial.println (Speed);
Serial.print ("Volts");
Serial.println (Volts,HEX);

delay(1000);




}
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: New user - Greenpower Car

Post by Basicmicro Support »

Current control is not an option on the Roboclaw. Current limiting should be set to a maximum current and left. Changing it on the fly is not the same a controller designed for current control.

Speed values only work when you have encoders installed and you need to make sure the encoders work using Motion Studio first.

You also need to know how to program in C++. You had some very basic errors(variable names didnt match, missing an argument from a commands, not using the proper argument types in another command.

Code: Select all

#include "RoboClaw.h"

RoboClaw roboclaw = RoboClaw(&Serial5, 10000);
uint16_t address = 0x80;
uint8_t status;
bool valid;

void setup() {
roboclaw.begin(38400);
Serial.begin(9600);
}




void loop() {

int16_t currentOne;
int16_t currentTwo;

roboclaw.SetM1DefaultAccel(0x80,2000 );
roboclaw.ForwardM1(0x80,00);


uint32_t Speed = roboclaw.ReadSpeedM1(address);
uint16_t Temp;
bool isvalid = roboclaw.ReadTemp(address,Temp);
uint16_t Volts = roboclaw.ReadMainBatteryVoltage(address);



bool gotIt = roboclaw.ReadCurrents(address, currentOne, currentTwo);
if(gotIt)
{
Serial.print("Hot diggity damn, we got the current data...");
Serial.print("Motor 1 is sucking up ");
Serial.print(currentOne);
Serial.println(" amp, or maybe that's milliamps...");
Serial.print("Motor 2 is sucking up ");
Serial.print(currentTwo);
Serial.println(" amp, or maybe that's milliamps...");
}

uint32_t speed1 = roboclaw.ReadSpeedM1(address,&status,&valid);
Serial.print("Speed1:");
Serial.print(speed1,DEC);
Serial.print(" ");
Serial.print ("Speed");
Serial.println (Speed);
Serial.print ("Volts");
Serial.println (Volts,HEX);

delay(1000);




}
I fixed the compile time errors. I wont do that again. I dont see any obvious logical errors but I also cant debug your code for your either.

Note I dont have whatever arduino you are using(no Serial5) so I tested with the hardware serial port set to Serial and then changed back when I posted the code.
marcopolo
Posts: 2
Joined: Tue Oct 20, 2020 3:32 pm
Re: New user - Greenpower Car

Post by marcopolo »

Thanks for this. Apologies for not getting back to you. I have had a bit of an enforced delay.Hospitals not the greatest place to be during Covid! So this is on hold a bit at the moment! Will keep you posted as to how it all goes .....
Apologies for the poor coding but I knew I was a long way off getting a compile and could not see the way through! I have another serial motor controller that is working but the code I wrote was very different (see below if interestesd), so in a way this was too easy LOL !!

void ReadMotor(int ControllerRead){ // reads both the data

//delay (20);
// motor command codes recieve
byte SpeedRead = 0x00; // 00 - 7F
byte CurrentControlRead = 0x36; // 12
byte InputVoltageRead = 0x10;
byte TempMOSFETTempRead= 0x0A;
byte TempControllerRead= 0x0B;
byte AccelerationRead = 0x04;
byte CurrentLimitRegisterRead = 0x02; //61ma per volt
byte TopSpeedRegisterRead = 0x21; //18.51 mv per unt 20v is 1080
//byte IRCompensationRegisterRead =0x20;
//byte SaveBits1read = 0x32;
//byte SaveBits2read = 0x33;
byte ConfigBits1read = 0x34;
byte ConfigBits2read = 0x35;
byte LockVariablesRegisterRead = 0x31;

MotorPort.flush();
while(MotorPort.available() > 0)
{
MotorPort.read();
}
//Serial.print ("ControllerRead");
//Serial.println (ControllerRead);

switch (ControllerRead) {
case 0: {
ControllerDataRead[1] = InputVoltageRead ;
Volt24reading = getMotorValues(); // * 0.01845; // convert the value into volts, THIS IS THE VALUE FROM THE DATASHEET.
Volt24true = Volt24reading * 0.01851;
// Serial.print("Voltage NEW = ");
// Serial.println(Volt24true);

}
// statements
break;

break;
case 1:{
ControllerDataRead[1] = TempMOSFETTempRead ;
MOSFETTemptrue = getMotorValues()-4; // no conv
// Serial.print("MOSFET Temp NEW = ");
// Serial.println(MOSFETTemptrue);

}
break;
case 2:{
ControllerDataRead[1] =SpeedRead ;
Speedreading = getMotorValues(); // * 0.01851; // convert the value into volts, THIS IS THE VALUE FROM THE DATASHEET.
Speedtrue = Speedreading;

}
break;

case 3:{
ControllerDataRead[1] = AccelerationRead ;
Accelerationreading = getMotorValues(); // * 0.01851; // convert the value into volts, THIS IS THE VALUE FROM THE DATASHEET.
Acceltrue = Accelerationreading;

}
break;
case 4:{
ControllerDataRead[1] = TempControllerRead ;
ControllerTemptrue = getMotorValues()-4; // * 0.01851; // convert the value into volts, THIS IS THE VALUE FROM THE DATASHEET.

}
break;

case 5:{
ControllerDataRead[1] = CurrentLimitRegisterRead ;
CurrentLimitValue = getMotorValues()*.061;

}
break;
case 6:{
if ( RPMMotor == 0 ){
ControllerDataRead[1] = CurrentControlRead ; //part 1 If throttle is Zero averages out the zero amp readings
float ZeroCurrentControllerreading = getMotorValues(); // * 0.01851; // convert the value into volts, THIS IS THE VALUE FROM THE DATASHEET.
SmoothZeroCurrentController = ZeroCurrentControllerSmoother.filter (ZeroCurrentControllerreading);

}
}
break;
case 7: {
ControllerDataRead[1] = CurrentControlRead ;
float CurrentControllerreading = getMotorValues(); // * 0.01851; // convert the value into volts, THIS IS THE VALUE FROM THE DATASHEET.
float SmoothCurrentControllerreading = CurrentControllerSmoother.filter (CurrentControllerreading);
CurrentControllertrue = (SmoothCurrentControllerreading-SmoothZeroCurrentController)*-.2442;

}

break;
case 8:{
ControllerDataRead[1] = TopSpeedRegisterRead ;
VoltLimitValue = getMotorValues()*.0342;
// Serial.print("VoltLimitValue = ");
// Serial.println(VoltLimitValue );
}
break;

case 9:{
ControllerDataRead[1] = LockVariablesRegisterRead ;
uint16_t LockVariables = getMotorValues();
// Serial.print("lock variables = ");
// Serial.println(LockVariables,BIN );

}
case 10:{
ControllerDataRead[1] = ConfigBits1read ;
uint16_t ConfigBits1 = getMotorValues();
// Serial.print("ConfigBits1 = ");
// Serial.println(ConfigBits1,BIN );

}
break;
case 11:{
ControllerDataRead[1] = ConfigBits2read ;
uint16_t ConfigBits2 = getMotorValues();
// Serial.print("ConfirBits2 = ");
// Serial.println(ConfigBits2,BIN );

}
break;
case 12:{
ControllerDataRead[1] = 0x0E ;
uint16_t FanSpeed = getMotorValues();
// Serial.print("FanSpeed = ");
// Serial.println(FanSpeed,DEC );

}
break;

default:
// nothing
break;
}
}
// ==================================================================================================================#
// repeated bit of code to get the data and decode it

float getMotorValues() {
char stuffBack [3];

ControllerDataRead[0] = ControllerAddress;
MotorPort.write (ControllerDataRead[0]);
MotorPort.write (ControllerDataRead[1]);
// Serial.print ("ControllerDataRead[0]");
// Serial.println (ControllerDataRead[0],HEX);
// Serial.println (ControllerDataRead[1],HEX);
delay (20);
if (MotorPort.available()) {

MotorPort.readBytes(stuffBack, 3);

// block for checking
// byte result = MotorPort.readBytes(stuffBack, 3); // read 3 bytes back from controller (last byte is CRC)
// Serial.print("\nRecieve command status = ");
// Serial.println(result);
// for(int loop = 0; loop < result; loop++) {// loop through recieved bytes
// Serial.print("Data = ");
// Serial.println(stuffBack[loop], BIN); // print the hex value of each byte, for debugging
// }
SerialValue=(stuffBack[0] << 7) + stuffBack[1]; // copy the first 2 bytes into the Value variable
// Serial.print("Value read = ");
// Serial.println(Value, BIN);// print the Value variable for debugging
}

return SerialValue;
}

Post Reply