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.
encoderBase.cpp@2:7f4be54c7257, 2018-02-16 (annotated)
- Committer:
- Weranest
- Date:
- Fri Feb 16 19:30:19 2018 +0000
- Revision:
- 2:7f4be54c7257
- Parent:
- 1:12f18cede014
- Child:
- 3:c9df852ad9ac
Just some encoder tweaks, need to finish that sometime
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Weranest | 2:7f4be54c7257 | 1 | #include "mbed.h" |
Weranest | 0:62e51b80d738 | 2 | #include "QEI.h" |
Weranest | 0:62e51b80d738 | 3 | #include "pins.cpp" |
Weranest | 0:62e51b80d738 | 4 | #define ENCODER_PULSE 256 //per manual |
Weranest | 0:62e51b80d738 | 5 | #define GEAR_RATIO |
Weranest | 1:12f18cede014 | 6 | #define SAMPLE_TIME 1.0f //user defined |
Weranest | 0:62e51b80d738 | 7 | #define WHEEL_DISTANCE //need to measure that |
Weranest | 0:62e51b80d738 | 8 | |
Weranest | 0:62e51b80d738 | 9 | //these lines setup the encoders for both wheels |
Weranest | 0:62e51b80d738 | 10 | QEI encoderRight(channelARight, channelBRight, channelIRight, ENCODER_PULSE); |
Weranest | 0:62e51b80d738 | 11 | QEI encoderLeft(channelALeft, channelBLeft, channelILeft, ENCODER_PULSE); |
Weranest | 0:62e51b80d738 | 12 | |
Weranest | 0:62e51b80d738 | 13 | //main wheel class, the subclasses should follow later |
Weranest | 0:62e51b80d738 | 14 | class Wheel{ |
Weranest | 0:62e51b80d738 | 15 | int currentPulses, previousPulses; |
Weranest | 0:62e51b80d738 | 16 | public: |
Weranest | 2:7f4be54c7257 | 17 | float velocity(){ |
Weranest | 2:7f4be54c7257 | 18 | return encoderVelocity*GEAR_RATIO; |
Weranest | 0:62e51b80d738 | 19 | } |
Weranest | 0:62e51b80d738 | 20 | private: |
Weranest | 1:12f18cede014 | 21 | float sampleTime= SAMPLE_TIME; |
Weranest | 0:62e51b80d738 | 22 | int gearRatio = GEAR_RATIO; |
Weranest | 2:7f4be54c7257 | 23 | float encoderVelocity(encoderPulse){ |
Weranest | 2:7f4be54c7257 | 24 | wait(sampleTIme); |
Weranest | 2:7f4be54c7257 | 25 | previousEncoderPulses=currentEncoderPulses; |
Weranest | 2:7f4be54c7257 | 26 | currentEncoderPulses=encoderPulse; |
Weranest | 0:62e51b80d738 | 27 | } |
Weranest | 1:12f18cede014 | 28 | |
Weranest | 0:62e51b80d738 | 29 | } wheelRight, wheelLeft; |
Weranest | 0:62e51b80d738 | 30 | |
Weranest | 0:62e51b80d738 | 31 | class WheelLeft |
Weranest | 0:62e51b80d738 | 32 | |
Weranest | 0:62e51b80d738 | 33 | //call this for speed, linear and angular |
Weranest | 0:62e51b80d738 | 34 | class Buggy{ |
Weranest | 0:62e51b80d738 | 35 | public: |
Weranest | 2:7f4be54c7257 | 36 | float speedLinear(){ |
Weranest | 2:7f4be54c7257 | 37 | return (wheelRight.velocity()+wheelLeft.velocity())/2; |
Weranest | 0:62e51b80d738 | 38 | } |
Weranest | 2:7f4be54c7257 | 39 | float speedAngular(){ |
Weranest | 2:7f4be54c7257 | 40 | return (wheelRight.velocity()-wheelLeft.velocity())/WHEEL_DISTANCE; |
Weranest | 0:62e51b80d738 | 41 | } |
Weranest | 2:7f4be54c7257 | 42 | }buggy; |
Weranest | 2:7f4be54c7257 | 43 | |
Weranest | 2:7f4be54c7257 | 44 | int speedMeasure(int encoderSide){ |
Weranest | 2:7f4be54c7257 | 45 | wheelRight.currentPulses(&encoderRight.getPulses()); |
Weranest | 2:7f4be54c7257 | 46 | wheelLeft.currentPulses(&encoderLeft.getPulses()); |
Weranest | 2:7f4be54c7257 | 47 | }//set this as an interupt routine on the timer based on SAMPLE_TIME |
Weranest | 0:62e51b80d738 | 48 | |
Weranest | 0:62e51b80d738 | 49 | |
Weranest | 2:7f4be54c7257 | 50 |