Try to fix more comms issues

This commit is contained in:
Alex Mikhalev 2019-06-02 10:29:22 -07:00
parent 429d195603
commit 8f3fdd137a
4 changed files with 38 additions and 8 deletions

View File

@ -27,7 +27,7 @@
// Definitions // Definitions
#define sampleFreqDef 512.0f // sample frequency in Hz #define sampleFreqDef 512.0f // sample frequency in Hz
#define betaDef 0.4f // 2 * proportional gain #define betaDef 0.2f // 2 * proportional gain
//============================================================================================ //============================================================================================
// Functions // Functions

View File

@ -118,6 +118,9 @@ void UGV::DoDebugPrint() {
mpu.accel.x, mpu.accel.y, mpu.accel.z, mpu.gyro_rate.x, mpu.accel.x, mpu.accel.y, mpu.accel.z, mpu.gyro_rate.x,
mpu.gyro_rate.y, mpu.gyro_rate.z, mpu.mag.x, mpu.mag.y, mpu.mag.z); mpu.gyro_rate.y, mpu.gyro_rate.z, mpu.mag.x, mpu.mag.y, mpu.mag.z);
#else #else
ESP_LOGD(TAG, "inputs: acc=(%f, %f, %f) gyro=(%f, %f, %f) mag=(%f, %f, %f)",
mpu.accel.x, mpu.accel.y, mpu.accel.z, mpu.gyro_rate.x,
mpu.gyro_rate.y, mpu.gyro_rate.z, mpu.mag.x, mpu.mag.y, mpu.mag.z);
ESP_LOGD(TAG, "ahrs: yaw=%f, pitch=%f, roll=%f", yaw_, pitch_, roll_); ESP_LOGD(TAG, "ahrs: yaw=%f, pitch=%f, roll=%f", yaw_, pitch_, roll_);
ESP_LOGV(TAG, "target angle: %f", angle_controller_.Setpoint()); ESP_LOGV(TAG, "target angle: %f", angle_controller_.Setpoint());
#endif #endif
@ -297,12 +300,13 @@ void UGV::OnTick() {
outputs_.right_motor = drive_power + angle_pwr; outputs_.right_motor = drive_power + angle_pwr;
} }
constexpr float MAX_ACCEL = 8 * LOOP_PERIOD_S; constexpr float MAX_ACCEL = 16 * LOOP_PERIOD_S;
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;
did_miss_mpu_ = true;
} 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_;
@ -335,6 +339,10 @@ void UGV::OnTick() {
#endif #endif
if (time_us >= last_print_ + debug_print_period) { if (time_us >= last_print_ + debug_print_period) {
if (did_miss_mpu_) {
ESP_LOGW(TAG, "no MPU data, disabling");
did_miss_mpu_ = false;
}
DoDebugPrint(); DoDebugPrint();
last_print_ = time_us; last_print_ = time_us;
} }

View File

@ -55,6 +55,7 @@ class UGV {
bool is_still_; bool is_still_;
float last_left_; float last_left_;
float last_right_; float last_right_;
bool did_miss_mpu_;
void UpdateAHRS(); void UpdateAHRS();
void DoDebugPrint(); void DoDebugPrint();

View File

@ -3,6 +3,7 @@
#include <esp_log.h> #include <esp_log.h>
#include <mbedtls/base64.h> #include <mbedtls/base64.h>
#include <rom/crc.h>
#include "messages.pb.h" #include "messages.pb.h"
@ -215,10 +216,24 @@ void CommsClass::HandlePacket(const uint8_t *data, size_t data_size) {
delete[] decoded; delete[] decoded;
return; return;
} }
if (decoded_len < 4) {
ESP_LOGE(TAG, "message too short (%d bytes)", decoded_len);
delete[] decoded;
return;
}
uint32_t calccrc = crc32_le(0, decoded, decoded_len - 4);
uint32_t msgcrc;
memcpy(&msgcrc, decoded + decoded_len - 4, 4);
if (calccrc != msgcrc) {
ESP_LOGW(TAG, "crc did not match (message %u, calculated %u)", msgcrc, calccrc);
delete[] decoded;
return;
}
GroundMessage ground_message; GroundMessage ground_message;
bool pb_ret = ground_message.ParseFromArray(decoded, decoded_len); bool pb_ret = ground_message.ParseFromArray(decoded, decoded_len - 4);
delete[] decoded; delete[] decoded;
if (!pb_ret) { if (!pb_ret) {
ESP_LOGE(TAG, "rx invalid protobuf"); ESP_LOGE(TAG, "rx invalid protobuf");
@ -316,17 +331,23 @@ esp_err_t CommsClass::SendPacket(const uint8_t *data, size_t size) {
ESP_LOGE(TAG, "SendPacket size too long: %d", size); ESP_LOGE(TAG, "SendPacket size too long: %d", size);
return ESP_ERR_INVALID_SIZE; return ESP_ERR_INVALID_SIZE;
} }
size_t encoded_size = ((size + 6) / 3) * 4; size_t fullsize = size + 4;
uint8_t *data_with_crc = new uint8_t[fullsize];
uint32_t crc = crc32_le(0, data, size);
ESP_LOGV(TAG, "Calculated crc value: %u", crc);
memcpy(data_with_crc, data, size);
memcpy(data_with_crc + size, &crc, 4);
size_t encoded_size = ((fullsize + 6) / 3) * 4;
uint8_t *encoded = new uint8_t[encoded_size]; uint8_t *encoded = new uint8_t[encoded_size];
size_t encoded_len; size_t encoded_len;
int ret = int ret =
mbedtls_base64_encode(encoded, encoded_size, &encoded_len, data, size); mbedtls_base64_encode(encoded, encoded_size, &encoded_len, data_with_crc, fullsize);
delete[] data_with_crc;
if (ret != 0) { if (ret != 0) {
delete[] encoded; delete[] encoded;
ESP_LOGE(TAG, "base64 encode error: %d", ret); ESP_LOGE(TAG, "base64 encode error: %d", ret);
return ESP_FAIL; return ESP_FAIL;
} }
// TODO: checksum
lora.WriteLn(encoded, encoded_len); lora.WriteLn(encoded, encoded_len);
delete[] encoded; delete[] encoded;
return ESP_OK; return ESP_OK;