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
Diff: arm.h
- Revision:
- 2:fc869e45e672
- Parent:
- 0:494acf21d3bc
- Child:
- 12:8295c02d740f
--- a/arm.h Mon Oct 31 13:05:53 2016 +0000
+++ b/arm.h Wed Nov 02 08:51:12 2016 +0000
@@ -1,4 +1,5 @@
#include "mbed.h"
+#include "QEI.h"
/*
Constants
@@ -7,10 +8,16 @@
const bool clockwise = true;
const bool counterClockwise = false;
-const float motorGain = 8.4f; // Rad/s
-const float maxVelocity = 8.4f; // Rad/s (max velocity: 80 rpm = 80*2*pi/60 = 8.4 rad/s)
-const float tick = 0.1f; // In seconds
-const float gearRadius = 5.2f; // In cm
+const float motorGain = 8.4f; // Rad/s
+const float maxVelocity = 8.4f; // Rad/s (max velocity: 80 rpm = 80*2*pi/60 = 8.4 rad/s)
+const float tick = 0.1f; // In seconds
+const float gearRadius = 5.2f; // In cm
+
+const float pi = 3.14159265359f;
+
+const float gearRatio = 1/131.25; // Gear ratio of the motor
+const int pulsesPerRevolution = 32; // Encoder pulses per revolution
+const float outputShaftResolution = pulsesPerRevolution/(gearRatio * 2 * pi); // In counts per rad
/*
Arm class
@@ -18,21 +25,39 @@
class Arm {
private:
- // Instance Variables
- float length; // Current length of the arm in centimeters
- float velocity; // Angular velocity in rotations per second
+ // Current length of the arm in centimeters
+ volatile float length;
+ // Reference angular velocity in rad/s
+ volatile float velocity;
+ // Real velocity according to the encoder
+ volatile float encoderVelocity;
+
+ // Motor PWM
PwmOut motorControl;
+ // Motor direction
DigitalOut motorDirection;
- Ticker ticker; // Keeps the arm's instance variables up to date
- // Methods
+ // Motor encoder
+ QEI qei;
+
+ // Directly instruct motor (0 <= motorValue <= 1)
void setMotor(float motorValue);
- void doTick();
+ // Read encoder angular velocity estimation
+ void updateEncoderVelocity();
public:
// Constructor
- Arm(float initialLength, PinName motorPWM, PinName motorDir);
- // Methods
+ Arm(float initialLength, PinName motorPWM, PinName motorDir, PinName encoderPin1, PinName encoderPin2);
+
+ // Update instance variables
+ void update();
+ // Get current arm length
float getLength();
+ // Get reference angular velocity
float getVelocity();
+ // Get encoder angular velocity estimation
+ float getEncoderVelocity();
+ // Set reference velocity
void setVelocity(float referenceVelocity);
+ // Stop moving
+ void kill();
};
\ No newline at end of file