Add motor speed control

This commit is contained in:
root 2022-03-21 22:37:13 +00:00
parent 1a488065ac
commit 5d8df998ae
5 changed files with 138 additions and 44 deletions

View file

@ -4,6 +4,7 @@
#include <cstdint>
#include <vector>
#include <chrono>
#include <thread>
#include "include/robot.hpp"
@ -18,6 +19,11 @@ public:
NONE = 5
};
enum Control : uint8_t {
POWER = 0,
SPEED = 1
};
static Motor& get_instance()
{
static Motor instance;
@ -27,18 +33,27 @@ public:
Motor(Motor const&) = delete;
void operator=(Motor const&) = delete;
static void power(uint8_t port, double percent);
static void pwm(uint8_t port, uint16_t pwm, Mode mode);
void set_speed(uint8_t port, double rpm);
void set_power(uint8_t port, double percent);
static void set_pwm(uint8_t port, uint16_t pwm, Mode mode);
std::vector<double> get_speed();
private:
Motor();
// Speed calculation and speed filter
int32_t last_encoder_values[MOTOR_COUNT] = {0};
double filtered_speeds[MOTOR_COUNT] = {0};
std::chrono::system_clock::time_point last_time_read;
std::chrono::system_clock::time_point last_time_encoders_read;
// Speed control
double speed_targets[MOTOR_COUNT] = {0};
double motor_control_modes[MOTOR_COUNT] = {0};
std::thread speed_control_thread;
void reset_speed();
void speed_control_loop();
};
#endif // COMPLIB_SERVER_MOTOR_HPP