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: Classic_PID iC_MU mbed-rtos mbed
TiltVelocityLoop.cpp@0:7ce0bc67f60f, 2015-04-02 (annotated)
- Committer:
- ms523
- Date:
- Thu Apr 02 07:35:39 2015 +0000
- Revision:
- 0:7ce0bc67f60f
- Child:
- 2:dc684c402296
Working as demo'd in the end of March sprint review.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ms523 | 0:7ce0bc67f60f | 1 | #include "mbed.h" |
| ms523 | 0:7ce0bc67f60f | 2 | #include "iC_MU.h" |
| ms523 | 0:7ce0bc67f60f | 3 | #include "rtos.h" |
| ms523 | 0:7ce0bc67f60f | 4 | #include "Classic_PID.h" |
| ms523 | 0:7ce0bc67f60f | 5 | |
| ms523 | 0:7ce0bc67f60f | 6 | // Define limits for zero crossing |
| ms523 | 0:7ce0bc67f60f | 7 | // These values should allow operation upto 3750 RPM |
| ms523 | 0:7ce0bc67f60f | 8 | #define bits 18 // The number of bits we want to use |
| ms523 | 0:7ce0bc67f60f | 9 | #define OneTurn (1<<bits) // 262144 counts per rev |
| ms523 | 0:7ce0bc67f60f | 10 | #define Lower (1<<(bits-5)) // 8192 counts = 11.25 degrees |
| ms523 | 0:7ce0bc67f60f | 11 | #define Upper OneTurn - Lower // 262144 - 8192 = 253952 |
| ms523 | 0:7ce0bc67f60f | 12 | |
| ms523 | 0:7ce0bc67f60f | 13 | extern iC_MU tilt_ic_mu; |
| ms523 | 0:7ce0bc67f60f | 14 | extern PwmOut Tilt_Motor_PWM; |
| ms523 | 0:7ce0bc67f60f | 15 | extern DigitalOut Tilt_Motor_Direction; |
| ms523 | 0:7ce0bc67f60f | 16 | extern Classic_PID TiltVelocityPID; |
| ms523 | 0:7ce0bc67f60f | 17 | |
| ms523 | 0:7ce0bc67f60f | 18 | int LastTiltPosition = 0; |
| ms523 | 0:7ce0bc67f60f | 19 | |
| ms523 | 0:7ce0bc67f60f | 20 | void TiltVelocityLoop(void const *args) |
| ms523 | 0:7ce0bc67f60f | 21 | { |
| ms523 | 0:7ce0bc67f60f | 22 | int Position = tilt_ic_mu.ReadPOSITION() >> (19 - bits);// Read the current position from the iC-MU and bitshift to reduce noise |
| ms523 | 0:7ce0bc67f60f | 23 | int Velocity = Position - LastTiltPosition; // Calculate change in position (i.e. Velocity) |
| ms523 | 0:7ce0bc67f60f | 24 | float Duty_Cycle = 0.0; |
| ms523 | 0:7ce0bc67f60f | 25 | |
| ms523 | 0:7ce0bc67f60f | 26 | // Check to see if we have gone past the index point |
| ms523 | 0:7ce0bc67f60f | 27 | if(Position < Lower & LastTiltPosition > Upper) { // We have gone over the index point in 1 direction |
| ms523 | 0:7ce0bc67f60f | 28 | Velocity += OneTurn; |
| ms523 | 0:7ce0bc67f60f | 29 | } else if(Position > Upper & LastTiltPosition < Lower) {// We have gone over the index point in the other direction |
| ms523 | 0:7ce0bc67f60f | 30 | Velocity -= OneTurn; |
| ms523 | 0:7ce0bc67f60f | 31 | } |
| ms523 | 0:7ce0bc67f60f | 32 | LastTiltPosition = Position; // Update new position from next time |
| ms523 | 0:7ce0bc67f60f | 33 | |
| ms523 | 0:7ce0bc67f60f | 34 | TiltVelocityPID.setProcessValue(Velocity); // Pass the Velocity onto the PID loop |
| ms523 | 0:7ce0bc67f60f | 35 | Duty_Cycle = TiltVelocityPID.compute(); |
| ms523 | 0:7ce0bc67f60f | 36 | |
| ms523 | 0:7ce0bc67f60f | 37 | if(Duty_Cycle < 0) { |
| ms523 | 0:7ce0bc67f60f | 38 | Tilt_Motor_Direction = 0; |
| ms523 | 0:7ce0bc67f60f | 39 | Tilt_Motor_PWM = 1 - (Duty_Cycle * -1.0); |
| ms523 | 0:7ce0bc67f60f | 40 | } else { |
| ms523 | 0:7ce0bc67f60f | 41 | Tilt_Motor_Direction = 1; |
| ms523 | 0:7ce0bc67f60f | 42 | Tilt_Motor_PWM = 1 - Duty_Cycle; |
| ms523 | 0:7ce0bc67f60f | 43 | } |
| ms523 | 0:7ce0bc67f60f | 44 | } |