uas-ugv/main/ugv_main.cc

109 lines
2.7 KiB
C++
Raw Normal View History

#include <esp_log.h>
2019-01-24 19:16:18 -08:00
#include <esp_timer.h>
2019-01-27 17:04:48 -08:00
#include "MadgwickAHRS.h"
#include "i2c_mutex.h"
2019-01-17 22:34:34 -08:00
#include "ugv_comms.hh"
#include "ugv_display.hh"
#include "ugv_io.hh"
2019-01-24 19:40:17 -08:00
#include <math.h>
namespace ugv {
2019-01-24 19:16:18 -08:00
using ugv::comms::CommsClass;
using ugv::io::IOClass;
2018-12-16 18:12:21 -08:00
static const char *TAG = "ugv_main";
2019-01-24 19:40:17 -08:00
extern "C" {
2019-01-27 17:04:48 -08:00
SemaphoreHandle_t i2c_mutex;
}
2019-01-24 19:40:17 -08:00
constexpr uint64_t LOOP_PERIOD_US = 1e6 / 100;
static const float PI = atanf(1.0) * 4.0;
2019-01-24 19:16:18 -08:00
extern "C" void OnTimeout(void *arg);
struct State {
public:
CommsClass * comms;
IOClass * io;
DisplayClass * display;
esp_timer_handle_t timer_handle;
io::Inputs inputs;
io::Outputs outputs;
2019-01-27 17:04:48 -08:00
int64_t last_print;
Madgwick ahrs_;
2019-01-24 19:16:18 -08:00
State() {
comms = new CommsClass();
io = new IOClass();
2019-01-24 19:40:17 -08:00
display = new DisplayClass(comms);
2019-01-24 19:16:18 -08:00
}
void Init() {
2019-01-24 23:00:12 -08:00
esp_timer_init();
i2c_mutex = xSemaphoreCreateMutex();
2019-01-24 23:00:12 -08:00
2019-01-27 17:04:48 -08:00
ahrs_.begin(1000000.f /
static_cast<float>(LOOP_PERIOD_US)); // rough sample frequency
2019-01-24 19:16:18 -08:00
comms->Init();
display->Init();
2019-01-27 17:04:48 -08:00
io->Init();
2018-12-20 19:26:59 -08:00
2019-01-24 19:16:18 -08:00
esp_timer_create_args_t timer_args;
2019-01-24 19:40:17 -08:00
timer_args.callback = OnTimeout;
timer_args.arg = this;
2019-01-24 19:16:18 -08:00
timer_args.dispatch_method = ESP_TIMER_TASK;
2019-01-24 19:40:17 -08:00
timer_args.name = "ugv_main_loop";
2019-01-24 19:16:18 -08:00
esp_timer_create(&timer_args, &this->timer_handle);
esp_timer_start_periodic(timer_handle, LOOP_PERIOD_US);
2019-01-24 23:00:12 -08:00
last_print = 0;
2019-01-24 19:16:18 -08:00
}
void OnTick() {
2019-01-24 23:00:12 -08:00
ESP_LOGV(TAG, "OnTick");
2019-01-24 19:40:17 -08:00
int64_t time_us = esp_timer_get_time();
float time_s = ((float)time_us) / 1e6;
io->ReadInputs(inputs);
outputs.left_motor = sinf(time_s * PI);
outputs.right_motor = cosf(time_s * PI);
io->WriteOutputs(outputs);
2019-01-27 17:04:48 -08:00
{
io::Vec3f &g = inputs.mpu.gyro_rate, &a = inputs.mpu.accel,
&m = inputs.mpu.mag;
ahrs_.update(g.x, g.y, g.z, a.x, a.y, a.z, m.x, m.y, m.z);
}
if (time_us >= last_print + 500 * 1000) { // 1s
2019-01-30 19:50:51 -08:00
ESP_LOGD(TAG,
2019-01-24 23:00:12 -08:00
"inputs: acc=(%f, %f, %f) gyro=(%f, %f, %f) mag=(%f, %f, %f)",
inputs.mpu.accel.x, inputs.mpu.accel.y, inputs.mpu.accel.z,
inputs.mpu.gyro_rate.x, inputs.mpu.gyro_rate.y,
inputs.mpu.gyro_rate.z, inputs.mpu.mag.x, inputs.mpu.mag.y,
inputs.mpu.mag.z);
2019-01-30 19:50:51 -08:00
ESP_LOGD(TAG, "ahrs: yaw=%f, pitch=%f, roll=%f", ahrs_.getYaw(),
2019-01-27 17:04:48 -08:00
ahrs_.getPitch(), ahrs_.getRoll());
2019-01-24 23:00:12 -08:00
last_print = time_us;
}
2019-01-24 19:16:18 -08:00
}
};
extern "C" void OnTimeout(void *arg) {
2019-01-24 19:40:17 -08:00
State *state = (State *)arg;
2019-01-24 19:16:18 -08:00
state->OnTick();
2018-12-21 09:20:30 -08:00
}
2018-12-20 19:26:59 -08:00
2019-01-24 19:16:18 -08:00
State *state;
2019-01-24 19:16:18 -08:00
void Setup(void) {
ESP_LOGI(TAG, "Starting UAS UGV");
state = new State();
state->Init();
ESP_LOGI(TAG, "Setup finished");
2018-12-31 01:40:41 -07:00
}
2019-01-24 19:16:18 -08:00
} // namespace ugv
extern "C" void app_main() { ugv::Setup(); }