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