Compare commits

..
4 changed files with 112 additions and 41 deletions

View file

@ -1,11 +1,11 @@
#pragma once
const float ACCEL_LIMIT = 5.0; // cm/s/s
const float VEL_LIMIT = 15.0; // cm/s
const float VEL_LIMIT = 16.0; // cm/s
const float BACKLASH_RIGHT = 0.0; // degrees
const float BACKLASH_LEFT = 0.0; // degrees
const float WHEEL_DIAMETER = 6.37; // cm
const float WHEEL_TO_WHEEL = 0.0; // cm
const float WHEEL_DIAMETER = 10.25; // cm
const float WHEEL_TO_WHEEL = 9.0; // cm
// centimeters per second to rpm
const float CMS_RPM = 60.0 / (PI*WHEEL_DIAMETER);
@ -13,6 +13,16 @@ const float CMS_RPM = 60.0 / (PI*WHEEL_DIAMETER);
// degrees to centimeters
const float DEG_CM = (PI*WHEEL_DIAMETER) / 360.0;
const float FORWARD_DISTANCE = 30.0; // cm
const float TURN_AMOUNT = 180.0; // degrees
const float FORWARD_DISTANCE = 10.0; // cm
const float TURN_AMOUNT = 175.0; // degrees
const float TURN_DISTANCE = (TURN_AMOUNT / 360.0) * WHEEL_TO_WHEEL * PI;
const float RETURN_DISTANCE = 100.0; // cm
const float FF_ACCEL = 2.3; // motor acceleration feedforward
const float FF_VEL = 0.7; // motor velocity feedforward
const float FF_STAT = 16.4; // motor static friction
const float KP = 2.0; // proportional
const float KI = 0.5; // integral
const float KD = 0.05; // derivative
const float KPP = 0.25; // position proportional
const float KPI = 0.05; // position integral

View file

@ -19,12 +19,28 @@ enum ROBOT_STATE {
float CURRENT_POSITION = 0.0;
float CURRENT_ROTATION = 0.0;
struct Setpoint setpoint;
float velocity = 40.0;
// start of current state
float phase_start;
enum ROBOT_STATE robot_state;
struct Trapezoidal forward = {VEL_LIMIT, ACCEL_LIMIT, FORWARD_DISTANCE};
struct Trapezoidal turn = {VEL_LIMIT, ACCEL_LIMIT, TURN_DISTANCE};
float left_iaccum = 0.0;
float right_iaccum = 0.0;
float time_p = 0.0;
float left_home = 0.0;
float right_home = 0.0;
void write_rpm_ff(SmartMotor* motor, int32_t rpm, float ff) {
if (rpm == 0) {rpm = 1;};
float kV = ff / (float) rpm; // calculate velocity feedforward that causes desired absolute feedforward
motor->tune_vel_pid(kV, KP,KI,KD);
delay(1);
motor->write_rpm(rpm);
delay(1);
}
void setup() {
// INIT SERIAL
@ -33,49 +49,73 @@ void setup() {
Wire.begin(); // INIT ARDUINO UNO AS I2C CONTROLLER
// TUNE VELOCITY PID
left_motor.tune_vel_pid(0.9, 3.7,0.3,0.0);
delay(10);
right_motor.tune_vel_pid(0.9, 3.7,0.3,0.0);
delay(10);
//right_motor.set_direction(PIDDirection::DIRECT);
delay(1000);
// READ MOTOR POSITION
int32_t pos = left_motor.read_angle();
Serial.print("Pos: ");
Serial.print(pos);
Serial.println(" deg");
left_motor.write_rpm(0.0);
delay(1);
right_motor.write_rpm(0.0);
delay(999);
phase_start = (float)millis() / 1000.0;
robot_state = FORWARD;
left_motor.home();
right_motor.home();
}
void loop() {
float time = (float)millis() / 1000.0 - phase_start;
float end_time;
switch (robot_state) {
case FORWARD:
case RETURN:
setpoint = trapezoidal_planner(&forward, time);
end_time = trapezoidal_time(&forward);
break;
case TURN:
break;
case RETURN:
setpoint = trapezoidal_planner(&turn, time);
end_time = trapezoidal_time(&turn);
break;
}
float turnsig = (robot_state == TURN ? 1.0 : -1.0); // direction of travel for right motor
// position PI controller
float pos_err_right = ((setpoint.position / DEG_CM) + right_home) - right_motor.read_angle();
float pos_err_left = ((turnsig * setpoint.position / DEG_CM) + left_home) - left_motor.read_angle();
left_motor.write_rpm(setpoint.velocity * CMS_RPM);
delay(10);
right_motor.write_rpm((robot_state == TURN ? velocity : -velocity) * CMS_RPM);
delay(10);
// delta should be ~4ms
if (time > time_p) {
float delta = time - time_p;
left_iaccum += pos_err_left * delta;
right_iaccum += pos_err_right * delta;
}
time_p = time;
// READ MOTOR POSITION
int32_t rpm = right_motor.read_rpm();
int32_t pos = right_motor.read_angle();
float right_effort = pos_err_right * KPP + right_iaccum * KPI;
float left_effort = pos_err_left * KPP + left_iaccum * KPI;
float right_velocity = setpoint.velocity + right_effort;
float left_velocity = (turnsig * setpoint.velocity) + left_effort;
// calculate feedforward from motion profile and position PI data
float feedforward_right =
setpoint.acceleration * CMS_RPM * FF_ACCEL +
right_velocity * CMS_RPM * FF_VEL +
((right_velocity > 0.0) ? FF_STAT : -FF_STAT);
float feedforward_left =
setpoint.acceleration * CMS_RPM * FF_ACCEL +
left_velocity * CMS_RPM * FF_VEL +
((left_velocity > 0.0) ? FF_STAT : -FF_STAT);
// arbitrary feedforward with on-board velocity PID
write_rpm_ff(&right_motor, right_velocity * CMS_RPM , feedforward_right);
write_rpm_ff(&left_motor, left_velocity * CMS_RPM , feedforward_left);
// send telemetry
int32_t rpm = left_motor.read_rpm();
int32_t pos = left_motor.read_angle();
int32_t error = setpoint.velocity * CMS_RPM - rpm;
Serial.print("time:");
Serial.print("ff:");
Serial.print(left_effort);
Serial.print(",time:");
Serial.print(time);
Serial.print(",distgoal:");
Serial.print(setpoint.position);
@ -83,29 +123,42 @@ void loop() {
Serial.print(pos * DEG_CM);
Serial.print(",cms/s:");
Serial.print(rpm / CMS_RPM);
Serial.print(",Setpoint:");
Serial.print(",setvel:");
Serial.print(setpoint.velocity);
Serial.print(",setacc:");
Serial.print(setpoint.acceleration);
Serial.print(",Err:");
Serial.print(error / CMS_RPM);
Serial.println("");
// move on if at setpoint TODO: check that the error is low as well
if (setpoint.complete) {
// total wheel offness in degrees
float total_pos_error = abs(pos_err_left) + abs(pos_err_right);
// move on if at setpoint TODO: give up after timeout
if (setpoint.complete && (total_pos_error < 3.0 || time - end_time > 3.0)) {
// rehome
right_home += (setpoint.position / DEG_CM);
left_home += (turnsig * setpoint.position / DEG_CM);
// zero accumulators
right_iaccum = 0.0; left_iaccum = 0.0;
switch (robot_state) {
case FORWARD:
robot_state = TURN;
phase_start = (float)millis() / 1000.0;
break;
case TURN:
robot_state = RETURN;
phase_start = (float)millis() / 1000.0;
// end (temp)
break;
case RETURN:
// end
left_motor.write_rpm(0.0);
delay(10);
delay(1);
right_motor.write_rpm(0.0);
for (;;) {};
break;
case TURN:
break;
case RETURN:
break;
}
}
}

View file

@ -8,6 +8,7 @@ struct Trapezoidal {
};
struct Setpoint {
float acceleration; // unitless
float velocity; // unitless
float position; // unitless
bool complete; // the setpoint will no longer change

View file

@ -6,7 +6,7 @@ struct Setpoint trapezoidal_planner(struct Trapezoidal* trapezoidal, float time)
float max_acc = trapezoidal->max_acc;
float dist = trapezoidal->dist;
struct Setpoint setpoint = {0.0, 0.0};
struct Setpoint setpoint = {0.0, 0.0, 0.0};
float time_accelerating = max_vel / max_acc;
float distance_while_accelerating = 0.5 * max_acc * time_accelerating * time_accelerating;
@ -18,16 +18,19 @@ struct Setpoint trapezoidal_planner(struct Trapezoidal* trapezoidal, float time)
if (time < peak_velocity_time) {
// accelerating
setpoint.acceleration = max_acc;
setpoint.velocity = max_acc * time;
setpoint.position = 0.5 * max_acc * time * time;
} else if (time < peak_velocity_time * 2.0) {
// slowing down
float time_decay = time - peak_velocity_time;
setpoint.acceleration = -max_acc;
setpoint.velocity = peak_velocity - (time_decay * max_acc);
setpoint.position = (0.5 * max_acc * peak_velocity_time * peak_velocity_time) // acceleration phase
+ peak_velocity * time_decay
- 0.5 * max_acc * time_decay * time_decay;
} else {
setpoint.acceleration = 0.0;
setpoint.velocity = 0;
setpoint.position = dist;
setpoint.complete = true;
@ -39,14 +42,17 @@ struct Setpoint trapezoidal_planner(struct Trapezoidal* trapezoidal, float time)
float total_time = 2 * time_accelerating + cruise_time;
if (time < time_accelerating) {
// still accelerating
setpoint.acceleration = max_acc;
setpoint.velocity = time * max_acc;
setpoint.position = 0.5 * max_acc * time * time;
} else if (time < time_accelerating + cruise_time) {
// cruising
setpoint.acceleration = 0.0;
setpoint.velocity = max_vel;
setpoint.position = distance_while_accelerating + max_vel * (time - time_accelerating);
} else if (time < total_time) {
// slowing down
setpoint.acceleration = -max_acc;
float time_decay = time - (time_accelerating + cruise_time);
setpoint.velocity = max_vel - (time_decay * max_acc);
setpoint.position = distance_while_accelerating + cruise_distance
@ -54,6 +60,7 @@ struct Setpoint trapezoidal_planner(struct Trapezoidal* trapezoidal, float time)
- 0.5 * max_acc * time_decay * time_decay;
} else {
//done
setpoint.acceleration = 0.0;
setpoint.velocity = 0.0;
setpoint.position = dist;
setpoint.complete = true;