|
|
@ -8,19 +8,19 @@ namespace ugv { |
|
|
|
|
|
|
|
|
|
|
|
static const char *TAG = "ugv_main"; |
|
|
|
static const char *TAG = "ugv_main"; |
|
|
|
|
|
|
|
|
|
|
|
constexpr uint64_t LOOP_PERIOD_US = 1e6 / 100; |
|
|
|
constexpr uint64_t LOOP_PERIOD_US = 1e6 / 100; |
|
|
|
constexpr float LOOP_HZ = 1000000.f / static_cast<float>(LOOP_PERIOD_US); |
|
|
|
constexpr float LOOP_HZ = 1000000.f / static_cast<float>(LOOP_PERIOD_US); |
|
|
|
constexpr float LOOP_PERIOD_S = 1 / LOOP_HZ; |
|
|
|
constexpr float LOOP_PERIOD_S = 1 / LOOP_HZ; |
|
|
|
constexpr TickType_t WATCHDOG_TICKS = pdMS_TO_TICKS(20 * 1000); |
|
|
|
constexpr TickType_t WATCHDOG_TICKS = pdMS_TO_TICKS(20 * 1000); |
|
|
|
|
|
|
|
|
|
|
|
constexpr uint64_t NOISE_PERIOD_US = 2000e3; |
|
|
|
constexpr uint64_t NOISE_PERIOD_US = 2000e3; |
|
|
|
constexpr float NOISE_PERIOD_S = static_cast<float>(NOISE_PERIOD_US) * 1.e-6f; |
|
|
|
constexpr float NOISE_PERIOD_S = static_cast<float>(NOISE_PERIOD_US) * 1.e-6f; |
|
|
|
|
|
|
|
|
|
|
|
constexpr float ACCEL_NOISE_THRESHOLD = 0.001; |
|
|
|
constexpr float ACCEL_NOISE_THRESHOLD = 0.001; |
|
|
|
constexpr float GYRO_NOISE_THRESHOLD = 0.3; |
|
|
|
constexpr float GYRO_NOISE_THRESHOLD = 0.3; |
|
|
|
|
|
|
|
|
|
|
|
void UpdateLocationFromGPS(comms::messages::Location &location, |
|
|
|
void UpdateLocationFromGPS(comms::messages::Location &location, |
|
|
|
const io::GpsData & gps_data) { |
|
|
|
const io::GpsData &gps_data) { |
|
|
|
location.set_fix_quality(gps_data.fix_quality); |
|
|
|
location.set_fix_quality(gps_data.fix_quality); |
|
|
|
location.set_latitude(gps_data.latitude); |
|
|
|
location.set_latitude(gps_data.latitude); |
|
|
|
location.set_longitude(gps_data.longitude); |
|
|
|
location.set_longitude(gps_data.longitude); |
|
|
@ -35,8 +35,8 @@ extern "C" void UGV_TickTimeout(void *arg) { |
|
|
|
UGV::UGV() : angle_controller_(LOOP_PERIOD_S) { |
|
|
|
UGV::UGV() : angle_controller_(LOOP_PERIOD_S) { |
|
|
|
SetTarget({34.069022, -118.443067}); |
|
|
|
SetTarget({34.069022, -118.443067}); |
|
|
|
|
|
|
|
|
|
|
|
comms = new CommsClass(); |
|
|
|
comms = new CommsClass(); |
|
|
|
io = new IOClass(); |
|
|
|
io = new IOClass(); |
|
|
|
display = new DisplayClass(comms); |
|
|
|
display = new DisplayClass(comms); |
|
|
|
|
|
|
|
|
|
|
|
SetConfig(DefaultConfig()); |
|
|
|
SetConfig(DefaultConfig()); |
|
|
@ -79,15 +79,15 @@ void UGV::Init() { |
|
|
|
display->Init(); |
|
|
|
display->Init(); |
|
|
|
|
|
|
|
|
|
|
|
esp_timer_create_args_t timer_args; |
|
|
|
esp_timer_create_args_t timer_args; |
|
|
|
timer_args.callback = UGV_TickTimeout; |
|
|
|
timer_args.callback = UGV_TickTimeout; |
|
|
|
timer_args.arg = this; |
|
|
|
timer_args.arg = this; |
|
|
|
timer_args.dispatch_method = ESP_TIMER_TASK; |
|
|
|
timer_args.dispatch_method = ESP_TIMER_TASK; |
|
|
|
timer_args.name = "ugv_main_loop"; |
|
|
|
timer_args.name = "ugv_main_loop"; |
|
|
|
esp_timer_create(&timer_args, &this->timer_handle); |
|
|
|
esp_timer_create(&timer_args, &this->timer_handle); |
|
|
|
esp_timer_start_periodic(timer_handle, LOOP_PERIOD_US); |
|
|
|
esp_timer_start_periodic(timer_handle, LOOP_PERIOD_US); |
|
|
|
last_print_ = 0; |
|
|
|
last_print_ = 0; |
|
|
|
last_noise_ = 0; |
|
|
|
last_noise_ = 0; |
|
|
|
last_left_ = 0; |
|
|
|
last_left_ = 0; |
|
|
|
last_right_ = 0; |
|
|
|
last_right_ = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -103,7 +103,7 @@ void UGV::UpdateAHRS() { |
|
|
|
yaw_ += 360.; |
|
|
|
yaw_ += 360.; |
|
|
|
} |
|
|
|
} |
|
|
|
pitch_ = ahrs_.getPitch(); |
|
|
|
pitch_ = ahrs_.getPitch(); |
|
|
|
roll_ = ahrs_.getRoll(); |
|
|
|
roll_ = ahrs_.getRoll(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void UGV::DoDebugPrint() { |
|
|
|
void UGV::DoDebugPrint() { |
|
|
@ -122,7 +122,7 @@ void UGV::ReadComms() { |
|
|
|
comms->status.set_pitch_angle(pitch_); |
|
|
|
comms->status.set_pitch_angle(pitch_); |
|
|
|
comms->status.set_roll_angle(roll_); |
|
|
|
comms->status.set_roll_angle(roll_); |
|
|
|
comms->status.set_is_still(is_still_); |
|
|
|
comms->status.set_is_still(is_still_); |
|
|
|
UGV_State comms_ugv_state = comms->status.state(); |
|
|
|
UGV_State comms_ugv_state = comms->status.state(); |
|
|
|
TickType_t ticks_since_last_packet = |
|
|
|
TickType_t ticks_since_last_packet = |
|
|
|
(xTaskGetTickCount() - comms->last_packet_tick); |
|
|
|
(xTaskGetTickCount() - comms->last_packet_tick); |
|
|
|
if (ticks_since_last_packet >= WATCHDOG_TICKS && |
|
|
|
if (ticks_since_last_packet >= WATCHDOG_TICKS && |
|
|
@ -160,31 +160,31 @@ void UGV::OnTick() { |
|
|
|
UpdateAHRS(); |
|
|
|
UpdateAHRS(); |
|
|
|
|
|
|
|
|
|
|
|
angle_controller_.Input(yaw_); |
|
|
|
angle_controller_.Input(yaw_); |
|
|
|
float drive_power = 0.; |
|
|
|
float drive_power = 0.; |
|
|
|
outputs_.left_motor = 0.0; |
|
|
|
outputs_.left_motor = 0.0; |
|
|
|
outputs_.right_motor = 0.0; |
|
|
|
outputs_.right_motor = 0.0; |
|
|
|
|
|
|
|
|
|
|
|
float pitch = roll_; // TODO: fix quaternion -> YPR
|
|
|
|
float pitch = roll_; // TODO: fix quaternion -> YPR
|
|
|
|
auto min_flip_pitch = conf_.min_flip_pitch(); |
|
|
|
auto min_flip_pitch = conf_.min_flip_pitch(); |
|
|
|
bool is_upside_down = (pitch > min_flip_pitch) || (pitch < -min_flip_pitch); |
|
|
|
bool is_upside_down = (pitch > min_flip_pitch) || (pitch < -min_flip_pitch); |
|
|
|
|
|
|
|
|
|
|
|
io::Vec3f d_accel = inputs_.mpu.accel - last_mpu_.accel; |
|
|
|
io::Vec3f d_accel = inputs_.mpu.accel - last_mpu_.accel; |
|
|
|
io::Vec3f d_gyro = inputs_.mpu.gyro_rate - last_mpu_.gyro_rate; |
|
|
|
io::Vec3f d_gyro = inputs_.mpu.gyro_rate - last_mpu_.gyro_rate; |
|
|
|
last_mpu_ = inputs_.mpu; |
|
|
|
last_mpu_ = inputs_.mpu; |
|
|
|
|
|
|
|
|
|
|
|
accel_noise_accum_ += d_accel.norm2() * LOOP_PERIOD_S; |
|
|
|
accel_noise_accum_ += d_accel.norm2() * LOOP_PERIOD_S; |
|
|
|
gyro_noise_accum_ += d_gyro.norm2() * LOOP_PERIOD_S; |
|
|
|
gyro_noise_accum_ += d_gyro.norm2() * LOOP_PERIOD_S; |
|
|
|
|
|
|
|
|
|
|
|
if (time_us >= last_noise_ + NOISE_PERIOD_US) { |
|
|
|
if (time_us >= last_noise_ + NOISE_PERIOD_US) { |
|
|
|
accel_noise_ = accel_noise_accum_ / NOISE_PERIOD_S; |
|
|
|
accel_noise_ = accel_noise_accum_ / NOISE_PERIOD_S; |
|
|
|
gyro_noise_ = gyro_noise_accum_ / NOISE_PERIOD_S; |
|
|
|
gyro_noise_ = gyro_noise_accum_ / NOISE_PERIOD_S; |
|
|
|
is_still_ = (accel_noise_ < ACCEL_NOISE_THRESHOLD) && |
|
|
|
is_still_ = (accel_noise_ < ACCEL_NOISE_THRESHOLD) && |
|
|
|
(gyro_noise_ < GYRO_NOISE_THRESHOLD); |
|
|
|
(gyro_noise_ < GYRO_NOISE_THRESHOLD); |
|
|
|
ESP_LOGD(TAG, "is still: %s, accel noise: %f, gyro noise: %f", |
|
|
|
ESP_LOGD(TAG, "is still: %s, accel noise: %f, gyro noise: %f", |
|
|
|
is_still_ ? "still" : "moving", accel_noise_, gyro_noise_); |
|
|
|
is_still_ ? "still" : "moving", accel_noise_, gyro_noise_); |
|
|
|
accel_noise_accum_ = 0; |
|
|
|
accel_noise_accum_ = 0; |
|
|
|
gyro_noise_accum_ = 0; |
|
|
|
gyro_noise_accum_ = 0; |
|
|
|
last_noise_ = time_us; |
|
|
|
last_noise_ = time_us; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ReadComms(); |
|
|
|
ReadComms(); |
|
|
@ -202,10 +202,10 @@ void UGV::OnTick() { |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
angle_controller_.Disable(); |
|
|
|
angle_controller_.Disable(); |
|
|
|
TickType_t current_tick = xTaskGetTickCount(); |
|
|
|
TickType_t current_tick = xTaskGetTickCount(); |
|
|
|
TickType_t ticks_since_gps = current_tick - inputs_.gps.last_update; |
|
|
|
TickType_t ticks_since_gps = current_tick - inputs_.gps.last_update; |
|
|
|
bool not_old = ticks_since_gps <= pdMS_TO_TICKS(2000); |
|
|
|
bool not_old = ticks_since_gps <= pdMS_TO_TICKS(2000); |
|
|
|
bool not_invalid = inputs_.gps.fix_quality != io::GPS_FIX_INVALID; |
|
|
|
bool not_invalid = inputs_.gps.fix_quality != io::GPS_FIX_INVALID; |
|
|
|
if (not_old && not_invalid) { |
|
|
|
if (not_old && not_invalid) { |
|
|
|
next_state_ = UGV_State::STATE_TURNING; |
|
|
|
next_state_ = UGV_State::STATE_TURNING; |
|
|
|
} |
|
|
|
} |
|
|
@ -213,7 +213,7 @@ void UGV::OnTick() { |
|
|
|
} |
|
|
|
} |
|
|
|
case UGV_State::STATE_FLIPPING: { |
|
|
|
case UGV_State::STATE_FLIPPING: { |
|
|
|
angle_controller_.Disable(); |
|
|
|
angle_controller_.Disable(); |
|
|
|
outputs_.left_motor = 0.8; |
|
|
|
outputs_.left_motor = 0.8; |
|
|
|
outputs_.right_motor = 0.8; |
|
|
|
outputs_.right_motor = 0.8; |
|
|
|
if (!is_upside_down) { |
|
|
|
if (!is_upside_down) { |
|
|
|
next_state_ = UGV_State::STATE_AQUIRING; |
|
|
|
next_state_ = UGV_State::STATE_AQUIRING; |
|
|
@ -232,7 +232,7 @@ void UGV::OnTick() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
LatLong current_pos = {inputs_.gps.latitude, inputs_.gps.longitude}; |
|
|
|
LatLong current_pos = {inputs_.gps.latitude, inputs_.gps.longitude}; |
|
|
|
float tgt_bearing = current_pos.bearing_toward(target_); |
|
|
|
float tgt_bearing = current_pos.bearing_toward(target_); |
|
|
|
angle_controller_.Enable(); |
|
|
|
angle_controller_.Enable(); |
|
|
|
angle_controller_.Setpoint(tgt_bearing); |
|
|
|
angle_controller_.Setpoint(tgt_bearing); |
|
|
|
|
|
|
|
|
|
|
@ -252,7 +252,7 @@ void UGV::OnTick() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
LatLong current_pos = {inputs_.gps.latitude, inputs_.gps.longitude}; |
|
|
|
LatLong current_pos = {inputs_.gps.latitude, inputs_.gps.longitude}; |
|
|
|
float tgt_dist = current_pos.distance_to(target_); |
|
|
|
float tgt_dist = current_pos.distance_to(target_); |
|
|
|
|
|
|
|
|
|
|
|
if (tgt_dist <= conf_.min_target_dist()) { |
|
|
|
if (tgt_dist <= conf_.min_target_dist()) { |
|
|
|
ESP_LOGI(TAG, "Finished driving to target"); |
|
|
|
ESP_LOGI(TAG, "Finished driving to target"); |
|
|
@ -268,7 +268,7 @@ void UGV::OnTick() { |
|
|
|
} |
|
|
|
} |
|
|
|
case UGV_State::STATE_TEST: |
|
|
|
case UGV_State::STATE_TEST: |
|
|
|
#ifdef BASIC_TEST |
|
|
|
#ifdef BASIC_TEST |
|
|
|
outputs.left_motor = sinf(time_s * PI); |
|
|
|
outputs.left_motor = sinf(time_s * PI); |
|
|
|
outputs.right_motor = cosf(time_s * PI); |
|
|
|
outputs.right_motor = cosf(time_s * PI); |
|
|
|
#else |
|
|
|
#else |
|
|
|
angle_controller_.Enable(); |
|
|
|
angle_controller_.Enable(); |
|
|
@ -283,8 +283,8 @@ void UGV::OnTick() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (angle_controller_.Enabled()) { |
|
|
|
if (angle_controller_.Enabled()) { |
|
|
|
float angle_pwr = angle_controller_.Update(); |
|
|
|
float angle_pwr = angle_controller_.Update(); |
|
|
|
outputs_.left_motor = drive_power - angle_pwr; |
|
|
|
outputs_.left_motor = drive_power - angle_pwr; |
|
|
|
outputs_.right_motor = drive_power + angle_pwr; |
|
|
|
outputs_.right_motor = drive_power + angle_pwr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -292,10 +292,10 @@ void UGV::OnTick() { |
|
|
|
|
|
|
|
|
|
|
|
if (inputs_.mpu.gyro_rate.x == 0 && inputs_.mpu.gyro_rate.y == 0 && |
|
|
|
if (inputs_.mpu.gyro_rate.x == 0 && inputs_.mpu.gyro_rate.y == 0 && |
|
|
|
inputs_.mpu.gyro_rate.z == 0) { |
|
|
|
inputs_.mpu.gyro_rate.z == 0) { |
|
|
|
outputs_.left_motor = 0; |
|
|
|
outputs_.left_motor = 0; |
|
|
|
outputs_.right_motor = 0; |
|
|
|
outputs_.right_motor = 0; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
float dleft = outputs_.left_motor - last_left_; |
|
|
|
float dleft = outputs_.left_motor - last_left_; |
|
|
|
float dright = outputs_.right_motor - last_right_; |
|
|
|
float dright = outputs_.right_motor - last_right_; |
|
|
|
if (dleft > MAX_ACCEL) { |
|
|
|
if (dleft > MAX_ACCEL) { |
|
|
|
outputs_.left_motor = last_left_ + MAX_ACCEL; |
|
|
|
outputs_.left_motor = last_left_ + MAX_ACCEL; |
|
|
@ -309,7 +309,7 @@ void UGV::OnTick() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
io->WriteOutputs(outputs_); |
|
|
|
io->WriteOutputs(outputs_); |
|
|
|
last_left_ = outputs_.left_motor; |
|
|
|
last_left_ = outputs_.left_motor; |
|
|
|
last_right_ = outputs_.right_motor; |
|
|
|
last_right_ = outputs_.right_motor; |
|
|
|
|
|
|
|
|
|
|
|
if (current_state_ != next_state_) { |
|
|
|
if (current_state_ != next_state_) { |
|
|
|