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):
12 qei(CH_A, CH_B, NC, pulsePerRev, QEI::X4_ENCODING),
13 pulse_per_rev(pulsePerRev),
15 update_rate(updateRate),
19 wheel_radius(wheelRadius)
21 PWM_pin.period(1.0 / pwm_freq);
30 prev_filtered_speed = 0;
36 curr_tick_count = qei.getPulses();
37 int tick_diff = curr_tick_count - prev_tick_count;
38 prev_tick_count = curr_tick_count;
41 rotational_freq = ((float) tick_diff / (4 * pulse_per_rev)) * update_rate;
44 rpm = rotational_freq * 60;
47 speed = 2 * pi * wheel_radius * rotational_freq;
50 filtered_speed = (prev_filtered_speed * LP_a0) + (speed * LP_b0) + (prev_speed * LP_b1);
52 prev_filtered_speed = filtered_speed;
66 prev_filtered_speed = 0;
72 duty_cycle = DutyCycle;
75 duty_cycle = -duty_cycle;
82 PWM_pin.write(1 - duty_cycle);
87 Direction.write(DirState);
93 Bipolar.write(BipState);
114 return curr_tick_count;
119 return rotational_freq;
134 return filtered_speed;
float get_speed(void)
Get the tangential speed of the wheel.
void set_bipolar_mode(bool BipState)
Set the bipolar mode of the motor.
void set_direction(bool DirState)
Set the direction of the motor.
float get_filtered_speed(void)
Get the low-pass filtered speed of the wheel.
void set_duty_cycle(float DutyCycle)
Set the duty cycle of the motor.
void update(void)
Calculate and update all the speed variables.
float get_rpm(void)
Get the revolutions per minute (RPM) of the motor.
float get_rotational_freq(void)
Get the rotational frequency of the motor in revolutions per second (rev/s).
float get_duty_cycle()
Get the duty cycle of the motor.
void reset(void)
Reset the encoder tick count.
int get_tick_count(void)
Get the cumulative tick count.
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.
bool get_bipolar_mode()
Get the bipolar mode of the motor.
bool get_direction()
Get the direction of the motor.
Motor and Encoder class library.