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: encoderBase.cpp
- Revision:
- 3:c9df852ad9ac
- Parent:
- 2:7f4be54c7257
- Child:
- 4:48d390356fba
diff -r 7f4be54c7257 -r c9df852ad9ac encoderBase.cpp --- a/encoderBase.cpp Fri Feb 16 19:30:19 2018 +0000 +++ b/encoderBase.cpp Wed Feb 21 11:55:29 2018 +0000 @@ -1,50 +1,35 @@ #include "mbed.h" #include "QEI.h" -#include "pins.cpp" -#define ENCODER_PULSE 256 //per manual -#define GEAR_RATIO -#define SAMPLE_TIME 1.0f //user defined -#define WHEEL_DISTANCE //need to measure that +#include "pins.h" +#include "constants.h" + //these lines setup the encoders for both wheels -QEI encoderRight(channelARight, channelBRight, channelIRight, ENCODER_PULSE); -QEI encoderLeft(channelALeft, channelBLeft, channelILeft, ENCODER_PULSE); //main wheel class, the subclasses should follow later class Wheel{ int currentPulses, previousPulses; public: - float velocity(){ - return encoderVelocity*GEAR_RATIO; + float velocity(int wheelPulses){ + return encoderVelocity(wheelPulses)*GEAR_RATIO; } private: - float sampleTime= SAMPLE_TIME; - int gearRatio = GEAR_RATIO; - float encoderVelocity(encoderPulse){ - wait(sampleTIme); - previousEncoderPulses=currentEncoderPulses; - currentEncoderPulses=encoderPulse; + float encoderVelocity(int encoderPulse){ + wait(SAMPLE_TIME); + previousPulses=currentPulses; + currentPulses=encoderPulse; + return (currentPulses-previousPulses)/SAMPLE_TIME; } } wheelRight, wheelLeft; -class WheelLeft - //call this for speed, linear and angular class Buggy{ public: - float speedLinear(){ - return (wheelRight.velocity()+wheelLeft.velocity())/2; + float speedLinear(int pulseRight, int pulseLeft){ + return (wheelRight.velocity(pulseRight)+wheelLeft.velocity(pulseLeft))/2; } - float speedAngular(){ - return (wheelRight.velocity()-wheelLeft.velocity())/WHEEL_DISTANCE; + float speedAngular(int pulseRight, int pulseLeft){ + return (wheelRight.velocity(pulseRight)-wheelLeft.velocity(pulseLeft))/WHEEL_DISTANCE; } }buggy; - -int speedMeasure(int encoderSide){ - wheelRight.currentPulses(&encoderRight.getPulses()); - wheelLeft.currentPulses(&encoderLeft.getPulses()); - }//set this as an interupt routine on the timer based on SAMPLE_TIME - - -