
Servo position controlled by potvalue
Dependencies: Servo
main.cpp@0:fe46ce7f03d3, 2017-10-02 (annotated)
- Committer:
- MMartens
- Date:
- Mon Oct 02 12:56:23 2017 +0000
- Revision:
- 0:fe46ce7f03d3
- Child:
- 1:34cea73289c4
Program for controlling the speed and position of the servo motor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MMartens | 0:fe46ce7f03d3 | 1 | /* This code controls the main functions of the servomotor. The servomotor needs an external power of 6 or 12 V and is connected to the PWMOut pin |
MMartens | 0:fe46ce7f03d3 | 2 | External power can be either 4*1.5V AA batteries (6V), 8*1.5V AA batteries (12V) or an appropiate DC voltage source. It is important that ground of both the servo motor and mBed platform are the same. |
MMartens | 0:fe46ce7f03d3 | 3 | Servo motor speed can be controlled by an external signal such as a potnetiometer or in context of this project an EMG signal. |
MMartens | 0:fe46ce7f03d3 | 4 | */ |
MMartens | 0:fe46ce7f03d3 | 5 | |
MMartens | 0:fe46ce7f03d3 | 6 | #include <mbed.h> |
MMartens | 0:fe46ce7f03d3 | 7 | //#include <Servo.h> either one can be used, but not both at the same time |
MMartens | 0:fe46ce7f03d3 | 8 | #include <VarSpeedServo.h> |
MMartens | 0:fe46ce7f03d3 | 9 | |
MMartens | 0:fe46ce7f03d3 | 10 | Servo myservo(p21); |
MMartens | 0:fe46ce7f03d3 | 11 | Serial pc(USBTX, USBRX); |
MMartens | 0:fe46ce7f03d3 | 12 | |
MMartens | 0:fe46ce7f03d3 | 13 | int main() { |
MMartens | 0:fe46ce7f03d3 | 14 | printf("Servo Calibration Controls:\n"); |
MMartens | 0:fe46ce7f03d3 | 15 | printf("1,2,3 - Position Servo (full left, middle, full right)\n"); |
MMartens | 0:fe46ce7f03d3 | 16 | printf("4,5 - Decrease or Increase range\n"); |
MMartens | 0:fe46ce7f03d3 | 17 | |
MMartens | 0:fe46ce7f03d3 | 18 | float range = 0.0005; |
MMartens | 0:fe46ce7f03d3 | 19 | float position = 0.5; |
MMartens | 0:fe46ce7f03d3 | 20 | |
MMartens | 0:fe46ce7f03d3 | 21 | while(1) { |
MMartens | 0:fe46ce7f03d3 | 22 | switch(pc.getc()) { // Switch to change position or increase range |
MMartens | 0:fe46ce7f03d3 | 23 | case '1': position = 0.0; break; |
MMartens | 0:fe46ce7f03d3 | 24 | case '2': position = 0.5; break; |
MMartens | 0:fe46ce7f03d3 | 25 | case '3': position = 1.0; break; |
MMartens | 0:fe46ce7f03d3 | 26 | case '4': range += 0.0001; break; |
MMartens | 0:fe46ce7f03d3 | 27 | case '5': range -= 0.0001; break; |
MMartens | 0:fe46ce7f03d3 | 28 | } |
MMartens | 0:fe46ce7f03d3 | 29 | printf("position = %.1f, range = +/-%0.4f\n", position, range); |
MMartens | 0:fe46ce7f03d3 | 30 | myservo.calibrate(range, 45.0); |
MMartens | 0:fe46ce7f03d3 | 31 | myservo = position; |
MMartens | 0:fe46ce7f03d3 | 32 | } |
MMartens | 0:fe46ce7f03d3 | 33 | } |
MMartens | 0:fe46ce7f03d3 | 34 | |
MMartens | 0:fe46ce7f03d3 | 35 | /* Example code to control the servomotor by a potentiometer, similarly one could control the speed of the servomotor. |
MMartens | 0:fe46ce7f03d3 | 36 | The speed can be controlled by using the library VarSpeedServo.h |
MMartens | 0:fe46ce7f03d3 | 37 | By mapping the intensity of EMG signal to a range of values (e.g. 0:1:100) you can easily control the speed and position of the motor |
MMartens | 0:fe46ce7f03d3 | 38 | */ |
MMartens | 0:fe46ce7f03d3 | 39 | |
MMartens | 0:fe46ce7f03d3 | 40 | /* |
MMartens | 0:fe46ce7f03d3 | 41 | Servo servo_test; //initialize a servo object for the connected servo |
MMartens | 0:fe46ce7f03d3 | 42 | |
MMartens | 0:fe46ce7f03d3 | 43 | int angle = 0; |
MMartens | 0:fe46ce7f03d3 | 44 | int potentio = A0; // initialize the A0analog pin for potentiometer |
MMartens | 0:fe46ce7f03d3 | 45 | |
MMartens | 0:fe46ce7f03d3 | 46 | |
MMartens | 0:fe46ce7f03d3 | 47 | void setup() |
MMartens | 0:fe46ce7f03d3 | 48 | { |
MMartens | 0:fe46ce7f03d3 | 49 | servo_test.attach(9); // attach the signal pin of servo to pin9 of arduino |
MMartens | 0:fe46ce7f03d3 | 50 | } |
MMartens | 0:fe46ce7f03d3 | 51 | |
MMartens | 0:fe46ce7f03d3 | 52 | void loop() |
MMartens | 0:fe46ce7f03d3 | 53 | { |
MMartens | 0:fe46ce7f03d3 | 54 | angle = analogRead(potentio); // reading the potentiometer value between 0 and 1023 |
MMartens | 0:fe46ce7f03d3 | 55 | angle = map(angle, 0, 1023, 0, 179); // scaling the potentiometer value to angle value for servo between 0 and 180) |
MMartens | 0:fe46ce7f03d3 | 56 | servo_test.write(angle); //command to rotate the servo to the specified angle |
MMartens | 0:fe46ce7f03d3 | 57 | delay(5); |
MMartens | 0:fe46ce7f03d3 | 58 | } |
MMartens | 0:fe46ce7f03d3 | 59 | */ |
MMartens | 0:fe46ce7f03d3 | 60 |