ESP Line Following Buggy
2nd Year Embedded System Project (Group 48 - 2023/24)
Loading...
Searching...
No Matches
motor.h
Go to the documentation of this file.
1
8#pragma once
9
10#include "mbed.h"
11#include "QEI.h"
12
21class Motor
22{
23private:
24
25 PwmOut PWM_pin; // creates a pulse-width-modulated(PWM) pin
26 DigitalOut Direction, Bipolar; // pins responsible for controlling direction and whether the H-bridge is bipolar or unipolar
27 float duty_cycle; // duty cycle of the PWM
28 bool direction; // boolean state of the direction pin
29 bool bipolar; // boolean state of the bipolar pin where: HIGH means its bipolar and LOW means its unipolar
30
31 QEI qei; // quadrature encoder object from an imported library
32 volatile int curr_tick_count; // the latest cumulative tick count recorded
33 volatile int prev_tick_count; // the cumulative tick count recorded before the latest cumulative tick count
34 volatile float rotational_freq; // rotational frequency of the wheel
35 volatile float speed; // latest tangential speed of the wheel before filtering out the noise
36 volatile float filtered_speed; // latest tangential speed of the wheel after filtering out the noise
37 volatile float prev_speed; // previous tangential speed of the wheel after filtering out the noise
38 volatile float prev_filtered_speed; // previous tangential speed of the wheel before filtering out the noise
39 volatile float rpm; // latest rounds per minute of the wheel
40
41 const int pwm_freq; // frequency at which the PWM is operating at
42 const int update_rate; // rate of which the values are updated
43 const int pulse_per_rev; // the tick counts counted by the encoder per revolution
44 const float wheel_radius; // radius of the buggy wheel
45
46 // Low pass filter constants
47 const float LP_a0;
48 const float LP_b0;
49 const float LP_b1;
50
51 // value of pi used
52 const float pi = 3.14159265;
53
54public:
55
72 Motor(PinName pwm, PinName dir, PinName bip, PinName CH_A, PinName CH_B,
73 int pulsePerRev, int pwmFreq, int updateRate, float LowPass_a0, float LowPass_b0, float LowPass_b1, float wheelRadius);
74
80 void set_direction(bool DirState);
81
87 void set_bipolar_mode(bool BipState);
88
94 void set_duty_cycle(float DutyCycle);
95
101 bool get_direction();
102
108 bool get_bipolar_mode();
109
115 float get_duty_cycle();
116
117 // Encoder Stuffs:
118
124 void update(void);
125
129 void reset(void);
130
136 int get_tick_count(void);
137
143 float get_rotational_freq(void);
144
150 float get_rpm(void);
151
157 float get_speed(void);
158
164 float get_filtered_speed(void);
165
166};
Represents a motor with integrated quadrature encoder for the buggy.
Definition motor.h:22
float get_speed(void)
Get the tangential speed of the wheel.
Definition motor.cpp:127
void set_bipolar_mode(bool BipState)
Set the bipolar mode of the motor.
Definition motor.cpp:91
void set_direction(bool DirState)
Set the direction of the motor.
Definition motor.cpp:85
float get_filtered_speed(void)
Get the low-pass filtered speed of the wheel.
Definition motor.cpp:132
void set_duty_cycle(float DutyCycle)
Set the duty cycle of the motor.
Definition motor.cpp:70
void update(void)
Calculate and update all the speed variables.
Definition motor.cpp:33
float get_rpm(void)
Get the revolutions per minute (RPM) of the motor.
Definition motor.cpp:122
float get_rotational_freq(void)
Get the rotational frequency of the motor in revolutions per second (rev/s).
Definition motor.cpp:117
float get_duty_cycle()
Get the duty cycle of the motor.
Definition motor.cpp:107
void reset(void)
Reset the encoder tick count.
Definition motor.cpp:56
int get_tick_count(void)
Get the cumulative tick count.
Definition motor.cpp:112
Motor(PinName pwm, PinName dir, PinName bip, PinName CH_A, PinName CH_B, int pulsePerRev, int pwmFreq, int updateRate, float LowPass_a0, float LowPass_b0, float LowPass_b1, float wheelRadius)
Construct a new Motor object.
Definition motor.cpp:7
bool get_bipolar_mode()
Get the bipolar mode of the motor.
Definition motor.cpp:102
bool get_direction()
Get the direction of the motor.
Definition motor.cpp:97