#include "ugv_io.hh" #include #include #include #include #include #include "MPU.hpp" #include "mpu/math.hpp" namespace ugv { namespace io { static constexpr mcpwm_unit_t MCPWM_UNIT = MCPWM_UNIT_0; static constexpr gpio_num_t MOTOR_LEFT_PWM = GPIO_NUM_0; static constexpr gpio_num_t MOTOR_LEFT_DIR = GPIO_NUM_0; static constexpr gpio_num_t MOTOR_RIGHT_PWM = GPIO_NUM_0; static constexpr gpio_num_t MOTOR_RIGHT_DIR = GPIO_NUM_0; IOClass IO; IOClass::IOClass() {} IOClass::~IOClass() {} void IOClass::Init() { InitMotors(); mpu_.Init(); gps_.Init(); } void IOClass::InitMotors() { gpio_set_direction(MOTOR_LEFT_PWM, GPIO_MODE_OUTPUT); gpio_set_direction(MOTOR_LEFT_DIR, GPIO_MODE_OUTPUT); gpio_set_direction(MOTOR_RIGHT_PWM, GPIO_MODE_OUTPUT); gpio_set_direction(MOTOR_RIGHT_DIR, GPIO_MODE_OUTPUT); mcpwm_gpio_init(MCPWM_UNIT, MCPWM0A, MOTOR_LEFT_PWM); mcpwm_gpio_init(MCPWM_UNIT, MCPWM0B, MOTOR_RIGHT_PWM); mcpwm_config_t mcpwm_config; mcpwm_config.frequency = 20000; // 20khz mcpwm_config.cmpr_a = 0; mcpwm_config.cmpr_b = 0; mcpwm_config.counter_mode = MCPWM_UP_DOWN_COUNTER; // for symmetric pwm mcpwm_config.duty_mode = MCPWM_DUTY_MODE_0; // active high mcpwm_init(MCPWM_UNIT, MCPWM_TIMER_0, &mcpwm_config); } void IOClass::ReadInputs(Inputs &inputs) { gps_.GetData(inputs.gps); mpu_.GetData(inputs.mpu); } void IOClass::WriteOutputs(const Outputs &outputs) { mcpwm_set_duty(MCPWM_UNIT, MCPWM_TIMER_0, MCPWM_OPR_A, fabs(outputs.left_motor)); mcpwm_set_duty(MCPWM_UNIT, MCPWM_TIMER_0, MCPWM_OPR_B, fabs(outputs.right_motor)); bool left_dir, right_dir; left_dir = outputs.left_motor < 0.f; right_dir = outputs.right_motor < 0.f; gpio_set_level(MOTOR_LEFT_DIR, left_dir); gpio_set_level(MOTOR_RIGHT_DIR, right_dir); } }; // namespace io }; // namespace ugv