Sergio Collado
/
servo_xperiment
An easy servo control program, controlling the servo position by a potenciometer
main.cpp@0:49ae6bc4220c, 2010-09-09 (annotated)
- Committer:
- Sergio
- Date:
- Thu Sep 09 01:33:16 2010 +0000
- Revision:
- 0:49ae6bc4220c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergio | 0:49ae6bc4220c | 1 | #include "mbed.h" |
Sergio | 0:49ae6bc4220c | 2 | |
Sergio | 0:49ae6bc4220c | 3 | //playing with servos... |
Sergio | 0:49ae6bc4220c | 4 | // Controlling the servo position with a potenciometer. |
Sergio | 0:49ae6bc4220c | 5 | |
Sergio | 0:49ae6bc4220c | 6 | // the servo must be powered from an external device, |
Sergio | 0:49ae6bc4220c | 7 | // since mbed, will cut it out when the current goes up 400mA. |
Sergio | 0:49ae6bc4220c | 8 | // just remember to ground the external batteries, chargers, ... |
Sergio | 0:49ae6bc4220c | 9 | // to the GND pin. |
Sergio | 0:49ae6bc4220c | 10 | |
Sergio | 0:49ae6bc4220c | 11 | // trick!: when control signal goes to 0V. the servo goes freely and dont |
Sergio | 0:49ae6bc4220c | 12 | // hold any stall torque!-> that could be useful sometimes, ... to load some |
Sergio | 0:49ae6bc4220c | 13 | // mechanism, or as a safety measurement. |
Sergio | 0:49ae6bc4220c | 14 | |
Sergio | 0:49ae6bc4220c | 15 | |
Sergio | 0:49ae6bc4220c | 16 | PwmOut myservo(p21); //PWM servo control signal goes here |
Sergio | 0:49ae6bc4220c | 17 | |
Sergio | 0:49ae6bc4220c | 18 | // ______ Position 0 degrees |
Sergio | 0:49ae6bc4220c | 19 | // | |_______________________________________________ |
Sergio | 0:49ae6bc4220c | 20 | // <-1ms-> 0.05%duty cycle |
Sergio | 0:49ae6bc4220c | 21 | // |
Sergio | 0:49ae6bc4220c | 22 | // __________________ Position 180 degrees |
Sergio | 0:49ae6bc4220c | 23 | // | |___________________________________ |
Sergio | 0:49ae6bc4220c | 24 | // <-------2.1ms-----> 0.1%aprox. duty cycle |
Sergio | 0:49ae6bc4220c | 25 | // |
Sergio | 0:49ae6bc4220c | 26 | // <----------------------------20ms----------------------> |
Sergio | 0:49ae6bc4220c | 27 | |
Sergio | 0:49ae6bc4220c | 28 | |
Sergio | 0:49ae6bc4220c | 29 | |
Sergio | 0:49ae6bc4220c | 30 | AnalogIn input(p20); //analog input is wired to a potenciometer, range 0->3.3V |
Sergio | 0:49ae6bc4220c | 31 | //and the value is represented as a float, range 0->1. |
Sergio | 0:49ae6bc4220c | 32 | |
Sergio | 0:49ae6bc4220c | 33 | //as the input range is from 0 to 1, and the PWM control, ranges is |
Sergio | 0:49ae6bc4220c | 34 | //from 0.05% to 0.1% is pretty easy make the control! |
Sergio | 0:49ae6bc4220c | 35 | //with the PWM .write() method,sets the duty cycle specified as a fload in range 0->1 |
Sergio | 0:49ae6bc4220c | 36 | //so, for the control only divide by 10 the input, is needed. |
Sergio | 0:49ae6bc4220c | 37 | |
Sergio | 0:49ae6bc4220c | 38 | |
Sergio | 0:49ae6bc4220c | 39 | //some ultra-light blue leds, for adding a cool effect! |
Sergio | 0:49ae6bc4220c | 40 | DigitalOut led1(LED1); |
Sergio | 0:49ae6bc4220c | 41 | DigitalOut led2(LED2); |
Sergio | 0:49ae6bc4220c | 42 | DigitalOut led3(LED3); |
Sergio | 0:49ae6bc4220c | 43 | DigitalOut led4(LED4); |
Sergio | 0:49ae6bc4220c | 44 | |
Sergio | 0:49ae6bc4220c | 45 | int main() { |
Sergio | 0:49ae6bc4220c | 46 | float ain; //ain-> Analog INput<- connected to a potenciometer for controlling the servo |
Sergio | 0:49ae6bc4220c | 47 | float period=20; |
Sergio | 0:49ae6bc4220c | 48 | |
Sergio | 0:49ae6bc4220c | 49 | myservo.period_ms(period); |
Sergio | 0:49ae6bc4220c | 50 | |
Sergio | 0:49ae6bc4220c | 51 | while (1) { |
Sergio | 0:49ae6bc4220c | 52 | |
Sergio | 0:49ae6bc4220c | 53 | ain=input.read();//the analog reading, is expressed as a float |
Sergio | 0:49ae6bc4220c | 54 | // with values from 0 to 1. 1 stands for max. voltage= 3.3V |
Sergio | 0:49ae6bc4220c | 55 | |
Sergio | 0:49ae6bc4220c | 56 | if (ain<0.02){ //if voltage is sooo low... |
Sergio | 0:49ae6bc4220c | 57 | led1=0; led2=0; led3=0; led4=0; //no leds! |
Sergio | 0:49ae6bc4220c | 58 | ain=0; //no control signal-> no torque, so you can |
Sergio | 0:49ae6bc4220c | 59 | //use it as safety measurement, or to manualy move the servo position. |
Sergio | 0:49ae6bc4220c | 60 | } |
Sergio | 0:49ae6bc4220c | 61 | else if ( ain>0.02 && ain<0.33){ // When the LED1 turns on, is because you have just hitted the 0 degrees position. |
Sergio | 0:49ae6bc4220c | 62 | led1=1; led2=0; led3=0; led4=0; // LED1 will be on from 0 to 60 degrees. ... aprox. :) |
Sergio | 0:49ae6bc4220c | 63 | } |
Sergio | 0:49ae6bc4220c | 64 | else if ( ain>0.33 && ain<0.66){ // LED2 switch on, when you pass the 60 degrees. |
Sergio | 0:49ae6bc4220c | 65 | led1=1; led2=1; led3=0; led4=0; |
Sergio | 0:49ae6bc4220c | 66 | } |
Sergio | 0:49ae6bc4220c | 67 | else if ( ain>0.66 && ain<0.95){ // LED3 switch on, when you pass 120 degrees |
Sergio | 0:49ae6bc4220c | 68 | led1=1; led2=1; led3=1; led4=0; |
Sergio | 0:49ae6bc4220c | 69 | } |
Sergio | 0:49ae6bc4220c | 70 | else { // LED4. you are at 180 degrees. |
Sergio | 0:49ae6bc4220c | 71 | led1=1; led2=2; led3=1; led4=1; |
Sergio | 0:49ae6bc4220c | 72 | } |
Sergio | 0:49ae6bc4220c | 73 | |
Sergio | 0:49ae6bc4220c | 74 | |
Sergio | 0:49ae6bc4220c | 75 | myservo.write(ain/10); |
Sergio | 0:49ae6bc4220c | 76 | |
Sergio | 0:49ae6bc4220c | 77 | } // end while(1) |
Sergio | 0:49ae6bc4220c | 78 | |
Sergio | 0:49ae6bc4220c | 79 | }//end main() |
Sergio | 0:49ae6bc4220c | 80 | |
Sergio | 0:49ae6bc4220c | 81 | |
Sergio | 0:49ae6bc4220c | 82 | |
Sergio | 0:49ae6bc4220c | 83 |