Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: RWR_Utils.hxx
- Revision:
- 0:3960679bf525
- Child:
- 1:755da47160cb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RWR_Utils.hxx Sun Jun 27 12:20:30 2021 +0000 @@ -0,0 +1,75 @@ +#pragma once + +#include "mbed.h" + +// pin assignment +#define ESP_RX PA_9 +#define ESP_TX PA_10 +#define ESP_RESET PA_12 +#define QTR_13 PB_0 +#define ESP_CHPD PB_7 +#define DRV_BENBL PB_6 +#define QTR_11 PB_1 +#define DRV_BPHASE PF_0 +#define QTR_CTL PF_1 +#define ESP_GPIO0 PA_8 +#define DRV_APHASE PA_11 +#define QTR_9 PA_7 +#define DRV_AENBL PA_4 +#define QTR_7 PA_3 +#define QTR_5 PA_1 +#define QTR_3 PA_0 + +// motor driver PWM frequency +#define PWM_FREQUENCY 32.0e3f +#define PWM_PERIOD 1.0f / PWM_FREQUENCY + +// number of sensors on QTR module +#define SENSORS_BAR_SIZE 6 + +// hysteresis thresholds for analog sensors +#define QTR_THRESHOLD_HIGH (uint32_t) 5000U +#define QTR_THRESHOLD_LOW (uint32_t) 20000U + +// PID parameters +#define PID_SETPOINT 0.0f +#define PID_KP 0.3f +#define PID_KD 0.0f +#define PID_KI 0.0001f + +class Motor +{ + public: + Motor(const PinName &_in1Pin, const PinName &_in2Pin, const bool _brake = false); + void setSpeed(const float speed); + void setBrake(const bool _brake); + + private: + PwmOut in1PWM; + PwmOut in2PWM; + bool brake; +}; + +class QTR +{ + public: + // QTR CTL pin, array of analog inputs connected to sensor bar, number of pulses applied to CTL pin + QTR(DigitalOut &_ctl, AnalogIn (&_sensors)[SENSORS_BAR_SIZE], const uint32_t dimPulses = 0); + + // dim sensors by applying pulses on CTL pin + void dim(const uint32_t pulses); + + // returns digitized sensor values as bits of an uint32_t + // a bit of 0 means that the corresponding sensor detects a reflective surface + // index 0 in sensors array corresponds to LSB in returned value + // uses hysteresis to reduce noise + uint32_t readSensorsAsDigital(); + + // return line position as a float between -1.0 (left) and 1.0 (right) + float readSensorsAsFloat(); + + private: + DigitalOut &ctl; + AnalogIn (&sensors)[SENSORS_BAR_SIZE]; + uint32_t lastSensorState; +};