Speed control is working properly using Motion Studio, motor runs properly using motion studio at different speeds and holds a set speed under changing load.
Just arrived AndMark PG188 Gearmotor with encoder with 7 pulses per revolution.
MotionStudio auto tune options gives PID values as follows:
P: 6.38650
I: 1.28910
D: 0.000
QPPS: 1125
Motor change direction, but speed is always the same.
Can You please share some example code that is working, thank You.
Example 1, speed set in range of +-QPPS (+-1500) where 10% speed is set as 150
Code: Select all
//See BareMinimum example for a list of library functions
//Includes required to use Roboclaw library
#include <LiquidCrystal.h>
#include <EEPROM.h>
#include <RoboClaw.h>
/*--------------------------------( Declare objects )------------------------------*/
// open hardware serial port on arduino //Setup communications with roboclaw with 1ms timeout
RoboClaw roboclaw(&Serial,10000);
#define address 0x80
//Velocity PID coefficients
#define Kp 6.38
#define Ki 1.28
#define Kd 0.00
#define qpps 1500
void setup() {
//Open Serial and roboclaw serial ports
Serial.begin(57600);
roboclaw.begin(38400);
//Set PID Coefficients
roboclaw.SetM1VelocityPID(address,Kd,Kp,Ki,qpps);
roboclaw.SetM2VelocityPID(address,Kd,Kp,Ki,qpps);
}
void displayspeed(void)
{
uint8_t status1,status2,status3,status4;
bool valid1,valid2,valid3,valid4;
int32_t enc1 = roboclaw.ReadEncM1(address, &status1, &valid1);
int32_t speed1 = roboclaw.ReadSpeedM1(address, &status3, &valid3);
Serial.print("Encoder1:");
if(valid1){
Serial.print(enc1,DEC);
Serial.print(" ");
Serial.print(status1,HEX);
Serial.print(" ");
}
else{
Serial.print("failed ");
}
Serial.print("Speed1:");
if(valid3){
Serial.print(speed1,DEC);
Serial.print(" ");
}
else{
Serial.print("failed ");
}
Serial.println();
}
void loop() {
uint8_t depth1,depth2;
roboclaw.SpeedDistanceM1(address,500,1500,1); // ------ (adress // + - speed in QPPS // dist in quadrature counts // flag )
do{
displayspeed();
roboclaw.ReadBuffers(address,depth1,depth2);
}while(depth1!=0x80); //Loop until distance command has completed
delay(1000);
roboclaw.SpeedDistanceM1(address,-1000,1500,1);
do{
displayspeed();
roboclaw.ReadBuffers(address,depth1,depth2);
}while(depth1!=0x80); //Loop until distance command has completed
delay(1000); //When no second command is given the motors will automatically slow down to 0 which takes 1 second
roboclaw.SpeedDistanceM1(address,1500,1500,1);
roboclaw.SpeedDistanceM1(address,-200,1500,0);
roboclaw.SpeedDistanceM1(address,0,1500,0);
do{
displayspeed();
roboclaw.ReadBuffers(address,depth1,depth2);
}while(depth1!=0x80); //Loop until distance command has completed
delay(1000);
}
Example 2, with same result:
Speed is in binary range +-32767, where 10% speed is given as 3276
Code: Select all
//See BareMinimum example for a list of library functions
//Includes required to use Roboclaw library
#include <LiquidCrystal.h>
#include <EEPROM.h>
#include <RoboClaw.h>
/*--------------------------------( Declare objects )------------------------------*/
// open hardware serial port on arduino //Setup communcaitions with roboclaw with0 m1s timeout
RoboClaw roboclaw(&Serial,10000);
#define address 0x80
//Velocity PID coefficients
#define Kp 6.38
#define Ki 1.28
#define Kd 0.00
#define qpps 1500
void setup() {
//Open Serial and roboclaw serial ports
Serial.begin(57600);
roboclaw.begin(38400);
//Set PID Coefficients
roboclaw.SetM1VelocityPID(address,Kd,Kp,Ki,qpps);
roboclaw.SetM2VelocityPID(address,Kd,Kp,Ki,qpps);
}
void displayspeed(void)
{
uint8_t status1,status2,status3,status4;
bool valid1,valid2,valid3,valid4;
int32_t enc1 = roboclaw.ReadEncM1(address, &status1, &valid1);
int32_t speed1 = roboclaw.ReadSpeedM1(address, &status3, &valid3);
Serial.print("Encoder1:");
if(valid1){
Serial.print(enc1,DEC);
Serial.print(" ");
Serial.print(status1,HEX);
Serial.print(" ");
}
else{
Serial.print("failed ");
}
Serial.print("Speed1:");
if(valid3){
Serial.print(speed1,DEC);
Serial.print(" ");
}
else{
Serial.print("failed ");
}
Serial.println();
}
void loop() {
uint8_t depth1,depth2;
roboclaw.SpeedDistanceM1(address,32767,3000,1); // ------ (adress // + - speed in percent of QPPS +-32767 // dist in quadrature counts // flag )
delay(5000);
roboclaw.SpeedDistanceM1(address,-3276,3000,1);
delay(5000); //When no second command is given the motors will automatically slow down to 0 which takes 1 second
roboclaw.SpeedDistanceM1(address,0,1500,0);
delay(6000);
}