ESP Line Following Buggy
2nd Year Embedded System Project (Group 48 - 2023/24)
Loading...
Searching...
No Matches
motor.cpp
Go to the documentation of this file.
1#include "mbed.h"
2
3#include "QEI.h"
4#include "motor.h"
5
6
7Motor::Motor(PinName pwm, PinName dir, PinName bip, PinName CH_A, PinName CH_B,
8 int pulsePerRev, int pwmFreq, int updateRate, float LowPass_a0, float LowPass_b0, float LowPass_b1, float wheelRadius):
9 PWM_pin(pwm),
10 Direction(dir),
11 Bipolar(bip),
12 qei(CH_A, CH_B, NC, pulsePerRev, QEI::X4_ENCODING),
13 pulse_per_rev(pulsePerRev),
14 pwm_freq(pwmFreq),
15 update_rate(updateRate),
16 LP_a0(LowPass_a0),
17 LP_b0(LowPass_b0),
18 LP_b1(LowPass_b1),
19 wheel_radius(wheelRadius)
20{
21 PWM_pin.period(1.0 / pwm_freq);
23 set_bipolar_mode(false);
25
26 prev_tick_count = 0;
27 speed = 0;
28 prev_speed = 0;
29 filtered_speed = 0;
30 prev_filtered_speed = 0;
31};
32
33void Motor::update(void)
34{
35 // update pulse diff
36 curr_tick_count = qei.getPulses();
37 int tick_diff = curr_tick_count - prev_tick_count;
38 prev_tick_count = curr_tick_count;
39
40 // update rotational freq
41 rotational_freq = ((float) tick_diff / (4 * pulse_per_rev)) * update_rate;
42
43 // update rpm
44 rpm = rotational_freq * 60;
45
46 // calculate raw speed
47 speed = 2 * pi * wheel_radius * rotational_freq;
48
49 // low pass filter for speed
50 filtered_speed = (prev_filtered_speed * LP_a0) + (speed * LP_b0) + (prev_speed * LP_b1);
51
52 prev_filtered_speed = filtered_speed;
53 prev_speed = speed;
54}
55
56void Motor::reset(void)
57{
58 qei.reset();
59
60 curr_tick_count = 0;
61 prev_tick_count = 0;
62 rotational_freq = 0;
63 speed = 0;
64 filtered_speed = 0;
65 prev_speed = 0;
66 prev_filtered_speed = 0;
67 rpm = 0;
68}
69
70void Motor::set_duty_cycle(float DutyCycle)
71{
72 duty_cycle = DutyCycle;
73 if (duty_cycle < 0.0)
74 {
75 duty_cycle = -duty_cycle;
77 }
78 else
79 {
81 }
82 PWM_pin.write(1 - duty_cycle);
83};
84
85void Motor::set_direction(bool DirState)
86{
87 Direction.write(DirState);
88 direction = DirState;
89};
90
91void Motor::set_bipolar_mode(bool BipState)
92{
93 Bipolar.write(BipState);
94 bipolar = BipState;
95};
96
98{
99 return direction;
100};
101
103{
104 return bipolar;
105};
106
108{
109 return duty_cycle;
110};
111
113{
114 return curr_tick_count;
115}
116
118{
119 return rotational_freq;
120}
121
122float Motor::get_rpm(void)
123{
124 return rpm;
125}
126
128{
129 return speed;
130}
131
133{
134 return filtered_speed;
135}
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
Motor and Encoder class library.