i trying code rover using pid meant go straight distance input , supposed turn. got rover go straight, can't turn reason. know in part:
this how motor goes forward. know make turn need change sign of 1 of motor drives such
i set 2 different function: 1 go forward , other turn. however, reason code doesn't read turn function. please help.
code: [select]
motor1drive((long)-pidval3*slope);
motor2drive((long)pidval4*slope);
this how motor goes forward. know make turn need change sign of 1 of motor drives such
code: [select]
motor1drive((long)-pidval3*slope);
motor2drive((long)-pidval4*slope);
i set 2 different function: 1 go forward , other turn. however, reason code doesn't read turn function. please help.
code: [select]
/* vriable decleration*/
//counters output pins
int counterinputs1[8] = {53,51,49,47,45,43,41,39}; // standard can connected differently depending on rover.
int counterinputs2[8] = {37,35,33,31,29,27,25,23};
//counter select pins
int countersel1 = 52;
int countersel2 = 32;
//counter enable pins
int counteroe1 = 50;
int counteroe2 = 30;
//counter reset pins
int counterrst1 = 48;
int counterrst2 = 28;
// motor pins
int motor1pwm = 3;
int motor1dir = 12;
int motor2pwm = 11;
int motor2dir = 13;
/* counterinit() method initalizes pin outputs encoders
* paramters:
* counterinputs is pointer array of input pin numbers 1 of hctl2016 encoder
* countersel pin number encoder "select" pin
* counteroe pin number encoder "output enable" pin
* counterrst pin number encoder "reset" pin
*/
void counter1init(int *counterinputs1, int countersel1, int counteroe1, int counterrst1)
{
pinmode(countersel1,output);
pinmode(counteroe1,output);
pinmode(counterrst1,output);
for(int = 0; < 8; i++)
{
pinmode(counterinputs1[i],input);
}
// reset counter
digitalwrite(counterrst1,low);
digitalwrite(counterrst1,high);
}
void counter2init(int *counterinputs2, int countersel2, int counteroe2, int counterrst2)
{
pinmode(countersel2,output);
pinmode(counteroe2,output);
pinmode(counterrst2,output);
for(int = 0; < 8; i++)
{
pinmode(counterinputs2[i],input);
}
// reset counter
digitalwrite(counterrst2,low);
digitalwrite(counterrst2,high);
}
/* counterread() method reads counts hctl2016 ic connected
* quadrature encoder , returns counter reading can transformed
* distance or angle.
* parameters:
* - counterinput[]: array of outputs of counter. first element represents
* least significant bit (lsb).
* - counteroe number of pin connected counter's enable pin.
* - countersel number of pin connected counter's select pin.
*
* return: number of counts of counter.
*/
void counterread(int inputs[8], int _oe, int _sel, int counterrst, long *counter_value)
{
long value = 0;
int readback[16];
bool signbit = 0;
long countervalue;
countervalue = *counter_value;
//enable counter
digitalwrite(_oe, low);
//select high byte
digitalwrite(_sel,low);
//read high byte counter
for (int = 8; < 15; i++)
{
value = value + (digitalread(inputs[i-8])<<i);
readback[i] = digitalread(inputs[i-8]);
}
readback[15] = digitalread(inputs[7]);
signbit = digitalread(inputs[7]);
//select low byte
digitalwrite(_sel,high);
//read low byte
for (int = 0; < 8; i++)
{
value = value + (digitalread(inputs[i])<<i);
readback[i] = digitalread(inputs[i]);
}
if(signbit)
{
value = value-32768;//why?
}
countervalue += value;
digitalwrite(_oe, high);
digitalwrite(counterrst,low);
digitalwrite(counterrst,high);
*counter_value = countervalue;
}
/* these methods drive motor
* usage instruction:
* - make sure 12v power source connected rover because less
* power sources may burn motor drive.
* - use putmotorvalue method copy , paste sketch
* , provide values want.
* paramters:
* val signed 9 bit number (-256, 255) indicates magnitude
* and direction of motor
*/
void motor1drive(long val)
{
val = constrain(val, -255,255);
//serial.print("motor val ");
//serial.print(val);
if (val >= 0 )
{
digitalwrite(motor1dir,high);
analogwrite(motor1pwm, val);
}
else
{
val = -val;
analogwrite(motor1dir,low);
analogwrite(motor1pwm,val);
}
}
void motor2drive(long val)
{
val = constrain(val, -255,255);
if (val >= 0 )
{
digitalwrite(motor2dir,high);
analogwrite(motor2pwm, val);
}
else
{
val = -val;
analogwrite(motor2dir,low);
analogwrite(motor2pwm,val);
}
}
/* motorinit() method initalizes pin outputs motors
* paramters:
* counterinputs is pointer array of input pin numbers 1 of hctl2016 encoder
* countersel pin number encoder "select" pin
* counteroe pin number encoder "output enable" pin
* counterrst pin number encoder "reset" pin
*/
void motor1init(int motor1pwm, int motor1dir)
{
pinmode(motor1pwm,output);
pinmode(motor1dir,output);
}
void motor2init(int motor2pwm, int motor2dir)
{
pinmode(motor2pwm,output);
pinmode(motor2dir,output);
}
/* differentiate() method
*/
float dt = 0;
float adelay = 0;
float differentiate(float input)
{
float t1 = 0;
float t2 = 0;
float diff_out = 0;
diff_out = input - adelay;
adelay = input;
dt = t1 - t2;
return diff_out;
}
/* integrate() method
*/
float sum = 0;
float previouserror;
float error;
float integrate(float input)
{
float t1 = 0;
float t2 = 0;
sum = input + sum;
dt = t2 - t1;
error = error - previouserror;
return sum;
}
/* pid() method
*/
float pid(float input, float kp, float ki, float kd)
{
return (kp*input + ki*integrate(input) + kd*differentiate(input));
}
/* setup() method
*/
void setup(){
//initializing arduino's pins
counter1init(counterinputs1, countersel1, counteroe1, counterrst1);
counter2init(counterinputs2, countersel2, counteroe2, counterrst2);
motor1init(motor1pwm,motor1dir);
motor2init(motor2pwm,motor2dir);
//start serial output
serial.begin(9600);
}
long countervalue1 = 0;
long countervalue2 = 0;
float readvalue1 = 0;
float readvalue2 = 0;
long pidval1 = 0;
long pidval2 = 0;
long pidval3 = 0;
long pidval4 = 0;
float u_t = 0; // u(t) = 5ft;
//float motorslope = .5;
float u_t2 = 0;
float t1 = 0;
float t2 = 0;
long dist[4] = {27000,194616,20000,276624};
+long distr[4] = {27000,194616,20000,276624};
int index = 3;
float slope = 0.1;
float kp1 =.64029;
float ki1 = 0;//.062678;
float kd1 = .069619;
float kp2 = 1.3568;
float ki2 = 0;//2.683;
float kd2 = .16614 ;
void firstline()
{
t1 = micros();
u_t = dist[index];
u_t2 = distr[index];
counterread(counterinputs1, counteroe1, countersel1, counterrst1, &countervalue1);
counterread(counterinputs2, counteroe2, countersel2, counterrst2, &countervalue2);
//void firstline(){
pidval1 = (long)(pid(u_t + countervalue1, kp1, ki1, kd1));
pidval2 = (long)-(pid(u_t - countervalue2, kp2, ki2, kd2));
motor1drive((long)-pidval1*slope);
motor2drive((long)pidval2*slope);
t2 = micros();
//serial.print("time ");
serial.print(t2);
serial.print("\t");
//serial.print("pid val ");
serial.print(countervalue1);
serial.print("\t");
serial.println(-
countervalue2);
// readvalue1 = 0;
// readvalue2 = 0;
// pidval = 0;
// pidval2 = 0;
// countervalue1 = 0;
// countervalue2 = 0;
}
void turn(){ //add delay? part
pidval3 = (long)(pid(u_t2 + countervalue1, kp1, ki1, kd1));
pidval4 = (long)-(pid(u_t2 - countervalue2, kp2, ki2, kd2));
// add delay? part
motor1drive((long)-pidval3*slope);
motor2drive((long)-pidval4*slope);
}
void loop() {
firstline();
//delay(4000);
turn();
//delay(4000);
}
snippets in isolation don't provide enough information. last code section entire sketch?
Arduino Forum > Topics > Robotics (Moderator: fabioc84) > Trying to get my rover to turn but it won't.
arduino
Comments
Post a Comment