Dependencies: PinDetect mbed Servo
Motor.cpp@0:81f78497df4e, 2012-06-18 (annotated)
- Committer:
- Rufaida
- Date:
- Mon Jun 18 17:47:17 2012 +0000
- Revision:
- 0:81f78497df4e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rufaida | 0:81f78497df4e | 1 | #include "Motor.h" |
Rufaida | 0:81f78497df4e | 2 | |
Rufaida | 0:81f78497df4e | 3 | /*definitions*/ |
Rufaida | 0:81f78497df4e | 4 | float sp_incr; |
Rufaida | 0:81f78497df4e | 5 | float current_sp; |
Rufaida | 0:81f78497df4e | 6 | int time_incr; |
Rufaida | 0:81f78497df4e | 7 | float speed_at_0_time; |
Rufaida | 0:81f78497df4e | 8 | Ticker Motor_Ticker; |
Rufaida | 0:81f78497df4e | 9 | Servo motor_speed(p26); |
Rufaida | 0:81f78497df4e | 10 | |
Rufaida | 0:81f78497df4e | 11 | /*Function for incrementing the speed and the time of the motor*/ |
Rufaida | 0:81f78497df4e | 12 | void motor(float fin_speed, int ramp_time) { |
Rufaida | 0:81f78497df4e | 13 | if (ramp_time) { |
Rufaida | 0:81f78497df4e | 14 | sp_incr = (fin_speed - current_sp) / float(ramp_time); |
Rufaida | 0:81f78497df4e | 15 | time_incr = ramp_time; |
Rufaida | 0:81f78497df4e | 16 | printf("Motor going to speed %f over %f seconds\n",fin_speed,float(ramp_time)/1000); |
Rufaida | 0:81f78497df4e | 17 | } else { |
Rufaida | 0:81f78497df4e | 18 | motor_speed = fin_speed; |
Rufaida | 0:81f78497df4e | 19 | printf("Motor at speed %f now\n",fin_speed); |
Rufaida | 0:81f78497df4e | 20 | current_sp = fin_speed; |
Rufaida | 0:81f78497df4e | 21 | } |
Rufaida | 0:81f78497df4e | 22 | |
Rufaida | 0:81f78497df4e | 23 | } |
Rufaida | 0:81f78497df4e | 24 | |
Rufaida | 0:81f78497df4e | 25 | /*This function is used to run the Ticker (update motor)*/ |
Rufaida | 0:81f78497df4e | 26 | void update_motor (void) { |
Rufaida | 0:81f78497df4e | 27 | if (time_incr) { |
Rufaida | 0:81f78497df4e | 28 | current_sp += sp_incr; |
Rufaida | 0:81f78497df4e | 29 | if (current_sp>1.0) { //If the speed is more than 1 it means it's 1 because the servo motor only accept 1 and 0. |
Rufaida | 0:81f78497df4e | 30 | current_sp=1; |
Rufaida | 0:81f78497df4e | 31 | } |
Rufaida | 0:81f78497df4e | 32 | if (current_sp<0.0) { //If the speed is less than 0 it will be equal to 0. |
Rufaida | 0:81f78497df4e | 33 | current_sp=0; |
Rufaida | 0:81f78497df4e | 34 | } |
Rufaida | 0:81f78497df4e | 35 | motor_speed = current_sp; |
Rufaida | 0:81f78497df4e | 36 | time_incr --; //the time that is fed to the motor will decrement and then it will start running. |
Rufaida | 0:81f78497df4e | 37 | } |
Rufaida | 0:81f78497df4e | 38 | } |
Rufaida | 0:81f78497df4e | 39 | |
Rufaida | 0:81f78497df4e | 40 | /*The Ticker function which runs the function above after a certain time continually*/ |
Rufaida | 0:81f78497df4e | 41 | void Starting_Motor (void) { |
Rufaida | 0:81f78497df4e | 42 | speed_at_0_time=0.5; //the motor is turned off at (time=0) and speed (0.5) |
Rufaida | 0:81f78497df4e | 43 | motor_speed=speed_at_0_time; |
Rufaida | 0:81f78497df4e | 44 | time_incr=0; |
Rufaida | 0:81f78497df4e | 45 | Motor_Ticker.attach(&update_motor,0.001); |
Rufaida | 0:81f78497df4e | 46 | } |