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.
Dependencies: mbed QEI biquadFilter
arm.h@2:fc869e45e672, 2016-11-02 (annotated)
- Committer:
- ronvbree
- Date:
- Wed Nov 02 08:51:12 2016 +0000
- Revision:
- 2:fc869e45e672
- Parent:
- 0:494acf21d3bc
- Child:
- 12:8295c02d740f
abcdg
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ronvbree | 0:494acf21d3bc | 1 | #include "mbed.h" |
| ronvbree | 2:fc869e45e672 | 2 | #include "QEI.h" |
| ronvbree | 0:494acf21d3bc | 3 | |
| ronvbree | 0:494acf21d3bc | 4 | /* |
| ronvbree | 0:494acf21d3bc | 5 | Constants |
| ronvbree | 0:494acf21d3bc | 6 | */ |
| ronvbree | 0:494acf21d3bc | 7 | |
| ronvbree | 0:494acf21d3bc | 8 | const bool clockwise = true; |
| ronvbree | 0:494acf21d3bc | 9 | const bool counterClockwise = false; |
| ronvbree | 0:494acf21d3bc | 10 | |
| ronvbree | 2:fc869e45e672 | 11 | const float motorGain = 8.4f; // Rad/s |
| ronvbree | 2:fc869e45e672 | 12 | const float maxVelocity = 8.4f; // Rad/s (max velocity: 80 rpm = 80*2*pi/60 = 8.4 rad/s) |
| ronvbree | 2:fc869e45e672 | 13 | const float tick = 0.1f; // In seconds |
| ronvbree | 2:fc869e45e672 | 14 | const float gearRadius = 5.2f; // In cm |
| ronvbree | 2:fc869e45e672 | 15 | |
| ronvbree | 2:fc869e45e672 | 16 | const float pi = 3.14159265359f; |
| ronvbree | 2:fc869e45e672 | 17 | |
| ronvbree | 2:fc869e45e672 | 18 | const float gearRatio = 1/131.25; // Gear ratio of the motor |
| ronvbree | 2:fc869e45e672 | 19 | const int pulsesPerRevolution = 32; // Encoder pulses per revolution |
| ronvbree | 2:fc869e45e672 | 20 | const float outputShaftResolution = pulsesPerRevolution/(gearRatio * 2 * pi); // In counts per rad |
| ronvbree | 0:494acf21d3bc | 21 | |
| ronvbree | 0:494acf21d3bc | 22 | /* |
| ronvbree | 0:494acf21d3bc | 23 | Arm class |
| ronvbree | 0:494acf21d3bc | 24 | */ |
| ronvbree | 0:494acf21d3bc | 25 | |
| ronvbree | 0:494acf21d3bc | 26 | class Arm { |
| ronvbree | 0:494acf21d3bc | 27 | private: |
| ronvbree | 2:fc869e45e672 | 28 | // Current length of the arm in centimeters |
| ronvbree | 2:fc869e45e672 | 29 | volatile float length; |
| ronvbree | 2:fc869e45e672 | 30 | // Reference angular velocity in rad/s |
| ronvbree | 2:fc869e45e672 | 31 | volatile float velocity; |
| ronvbree | 2:fc869e45e672 | 32 | // Real velocity according to the encoder |
| ronvbree | 2:fc869e45e672 | 33 | volatile float encoderVelocity; |
| ronvbree | 2:fc869e45e672 | 34 | |
| ronvbree | 2:fc869e45e672 | 35 | // Motor PWM |
| ronvbree | 0:494acf21d3bc | 36 | PwmOut motorControl; |
| ronvbree | 2:fc869e45e672 | 37 | // Motor direction |
| ronvbree | 0:494acf21d3bc | 38 | DigitalOut motorDirection; |
| ronvbree | 2:fc869e45e672 | 39 | // Motor encoder |
| ronvbree | 2:fc869e45e672 | 40 | QEI qei; |
| ronvbree | 2:fc869e45e672 | 41 | |
| ronvbree | 2:fc869e45e672 | 42 | // Directly instruct motor (0 <= motorValue <= 1) |
| ronvbree | 0:494acf21d3bc | 43 | void setMotor(float motorValue); |
| ronvbree | 2:fc869e45e672 | 44 | // Read encoder angular velocity estimation |
| ronvbree | 2:fc869e45e672 | 45 | void updateEncoderVelocity(); |
| ronvbree | 0:494acf21d3bc | 46 | public: |
| ronvbree | 0:494acf21d3bc | 47 | // Constructor |
| ronvbree | 2:fc869e45e672 | 48 | Arm(float initialLength, PinName motorPWM, PinName motorDir, PinName encoderPin1, PinName encoderPin2); |
| ronvbree | 2:fc869e45e672 | 49 | |
| ronvbree | 2:fc869e45e672 | 50 | // Update instance variables |
| ronvbree | 2:fc869e45e672 | 51 | void update(); |
| ronvbree | 2:fc869e45e672 | 52 | // Get current arm length |
| ronvbree | 0:494acf21d3bc | 53 | float getLength(); |
| ronvbree | 2:fc869e45e672 | 54 | // Get reference angular velocity |
| ronvbree | 0:494acf21d3bc | 55 | float getVelocity(); |
| ronvbree | 2:fc869e45e672 | 56 | // Get encoder angular velocity estimation |
| ronvbree | 2:fc869e45e672 | 57 | float getEncoderVelocity(); |
| ronvbree | 2:fc869e45e672 | 58 | // Set reference velocity |
| ronvbree | 0:494acf21d3bc | 59 | void setVelocity(float referenceVelocity); |
| ronvbree | 2:fc869e45e672 | 60 | // Stop moving |
| ronvbree | 2:fc869e45e672 | 61 | void kill(); |
| ronvbree | 0:494acf21d3bc | 62 | |
| ronvbree | 0:494acf21d3bc | 63 | }; |