diff --git a/robot_controller/constants.h b/robot_controller/constants.h index 4498c1c..60c34fb 100644 --- a/robot_controller/constants.h +++ b/robot_controller/constants.h @@ -1,11 +1,11 @@ #pragma once const float ACCEL_LIMIT = 5.0; // cm/s/s -const float VEL_LIMIT = 16.0; // cm/s +const float VEL_LIMIT = 15.0; // cm/s const float BACKLASH_RIGHT = 0.0; // degrees const float BACKLASH_LEFT = 0.0; // degrees -const float WHEEL_DIAMETER = 10.25; // cm -const float WHEEL_TO_WHEEL = 9.0; // cm +const float WHEEL_DIAMETER = 6.37; // cm +const float WHEEL_TO_WHEEL = 0.0; // cm // centimeters per second to rpm const float CMS_RPM = 60.0 / (PI*WHEEL_DIAMETER); @@ -13,16 +13,6 @@ 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 = 10.0; // cm -const float TURN_AMOUNT = 175.0; // degrees -const float TURN_DISTANCE = (TURN_AMOUNT / 360.0) * WHEEL_TO_WHEEL * PI; +const float FORWARD_DISTANCE = 30.0; // cm +const float TURN_AMOUNT = 180.0; // degrees 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 diff --git a/robot_controller/robot_controller.ino b/robot_controller/robot_controller.ino index 1f99668..45c63fe 100644 --- a/robot_controller/robot_controller.ino +++ b/robot_controller/robot_controller.ino @@ -19,28 +19,12 @@ 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 @@ -49,116 +33,79 @@ void setup() { Wire.begin(); // INIT ARDUINO UNO AS I2C CONTROLLER - left_motor.write_rpm(0.0); - delay(1); - right_motor.write_rpm(0.0); - delay(999); + // 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"); + 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: - setpoint = trapezoidal_planner(&turn, time); - end_time = trapezoidal_time(&turn); + break; + case RETURN: 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(); - // 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; + left_motor.write_rpm(setpoint.velocity * CMS_RPM); + delay(10); + right_motor.write_rpm((robot_state == TURN ? velocity : -velocity) * CMS_RPM); + delay(10); - 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(); + // READ MOTOR POSITION + int32_t rpm = right_motor.read_rpm(); + int32_t pos = right_motor.read_angle(); int32_t error = setpoint.velocity * CMS_RPM - rpm; - Serial.print("ff:"); - Serial.print(left_effort); - Serial.print(",time:"); + Serial.print("time:"); Serial.print(time); - Serial.print(",distgoal:"); + Serial.print(",dist goal:"); Serial.print(setpoint.position); Serial.print(",dist:"); Serial.print(pos * DEG_CM); Serial.print(",cms/s:"); Serial.print(rpm / CMS_RPM); - Serial.print(",setvel:"); + Serial.print(",Setpoint:"); Serial.print(setpoint.velocity); - Serial.print(",setacc:"); - Serial.print(setpoint.acceleration); Serial.print(",Err:"); Serial.print(error / CMS_RPM); Serial.println(""); - // 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; - + // move on if at setpoint TODO: check that the error is low as well + if (setpoint.complete) { 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; - break; - case RETURN: - - // end + // end (temp) left_motor.write_rpm(0.0); - delay(1); + delay(10); right_motor.write_rpm(0.0); for (;;) {}; break; + case TURN: + break; + case RETURN: + break; } } } diff --git a/robot_controller/trapezoidal.h b/robot_controller/trapezoidal.h index 27e4edf..a991f67 100644 --- a/robot_controller/trapezoidal.h +++ b/robot_controller/trapezoidal.h @@ -8,7 +8,6 @@ struct Trapezoidal { }; struct Setpoint { - float acceleration; // unitless float velocity; // unitless float position; // unitless bool complete; // the setpoint will no longer change diff --git a/robot_controller/trapezoidal.ino b/robot_controller/trapezoidal.ino index a1ce35a..847d608 100644 --- a/robot_controller/trapezoidal.ino +++ b/robot_controller/trapezoidal.ino @@ -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, 0.0}; + struct Setpoint setpoint = {0.0, 0.0}; float time_accelerating = max_vel / max_acc; float distance_while_accelerating = 0.5 * max_acc * time_accelerating * time_accelerating; @@ -18,19 +18,16 @@ 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; @@ -42,17 +39,14 @@ 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 @@ -60,7 +54,6 @@ 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;