ME 507 Romi++
Cylindrical Differential Drive Robot created for ME 507 at Cal Poly in Spring 2025
Loading...
Searching...
No Matches
motor_task.h
Go to the documentation of this file.
1/*
2 * motor_task.h
3 *
4 * Created on: Jun 2, 2025
5 * Author: jacka
6 */
7
8#ifndef INC_MOTOR_TASK_H_
9#define INC_MOTOR_TASK_H_
10
18
19#include "motor_driver.h"
20#include "encoder.h"
21#include "bluetooth.h"
22#include "command_parser.h"
23#include "heading.h"
24#include "balance.h"
25#include "time_utils.h"
26#include <stdint.h>
27#include <algorithm>
28
30extern TIM_HandleTypeDef htim1;
32extern TIM_HandleTypeDef htim3;
34extern TIM_HandleTypeDef htim4;
36extern TIM_HandleTypeDef htim5;
37
39extern BLUETOOTH bluetooth;
40
42extern COMMAND_PARSER parser;
43
45extern HEADING heading;
46
48extern BALANCE balance;
49
51extern int32_t AR;
52
60{
61public:
66 typedef enum state
67 {
71 } state_t;
72
76 MOTOR_TASK();
77
82 void run(void);
83
84private:
85 state_t state;
86 bool should_run();
87
88 // Motor hardware abstraction
89 motor_t motor1;
90 motor_t motor2;
91
92 float M1_eff;
93 float M2_eff;
94
95 int16_t M1_set;
96 int16_t M2_set;
97
98 // Encoder feedback
99 encoder_t encoder1;
100 encoder_t encoder2;
101
102 // PID gains for motor 1
103 float Kp1;
104 float Ki1;
105 float Kd1;
106
107 // PID state variables for motor 1
108 int32_t error1;
109 int32_t prev_error1;
110 int32_t error_diff1;
111 int32_t error_sum1;
112
113 // PID gains for motor 2
114 float Kp2;
115 float Ki2;
116 float Kd2;
117
118 // PID state variables for motor 2
119 int32_t error2;
120 int32_t prev_error2;
121 int32_t error_diff2;
122 int32_t error_sum2;
123
124 // Timing variables
125 uint32_t dt;
126 uint32_t now;
127 uint32_t last_time;
128};
129
130#endif /* INC_MOTOR_TASK_H_ */
Declaration of the BALANCE class for managing robot balance using a FSM.
Bluetooth command handler for robot control.
Finite State Machine class for robot balance control.
Definition balance.h:35
Finite State Machine for Bluetooth-based control input.
Definition bluetooth.h:31
Finite State Machine for heading control based on IMU data.
Definition heading.h:39
void run(void)
Executes one iteration of the motor control task.
Definition motor_task.cpp:41
state
Definition motor_task.h:67
@ S2_RUN
Active motor control loop.
Definition motor_task.h:70
@ S1_IDLE
Idle (standby) state.
Definition motor_task.h:69
@ S0_INIT
Initialization state.
Definition motor_task.h:68
MOTOR_TASK()
Constructor for the MOTOR_TASK class.
Definition motor_task.cpp:14
Header for UART command parsing module.
Quadrature encoder interface for STM32 timers.
FSM class for heading-based robot control using IMU input.
COMMAND_PARSER parser
External reference to the command parser.
Definition main.cpp:97
Low-level motor control interface for STM32 using PWM.
TIM_HandleTypeDef htim1
Timer handle for PWM generation on motor 1.
Definition main.cpp:74
TIM_HandleTypeDef htim4
Timer handle for encoder 2 (e.g., right wheel).
Definition main.cpp:77
BLUETOOTH bluetooth
Global reference to Bluetooth control FSM.
Definition main.cpp:90
TIM_HandleTypeDef htim3
Timer handle for encoder 1 (e.g., left wheel).
Definition main.cpp:76
int32_t AR
Global auto-reload value for timers.
Definition main.cpp:111
HEADING heading
Global reference to heading control task.
Definition main.cpp:87
TIM_HandleTypeDef htim5
Timer handle for motor 2 PWM generation.
Definition main.cpp:78
BALANCE balance
Global reference to balance control task.
Definition main.cpp:88
Structure for tracking encoder state and motion data.
Definition encoder.h:31
Struct representing a DC motor controlled by two PWM channels.
Definition motor_driver.h:31
Utility functions for microsecond-level timing.