![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Servo script + magnet driver for biorobotics project in Uni. Made by Teun van der Molen
Dependencies: MODSERIAL Servo mbed
main.cpp@1:26baa438620b, 2015-09-28 (annotated)
- Committer:
- teunman
- Date:
- Mon Sep 28 14:27:06 2015 +0000
- Revision:
- 1:26baa438620b
- Parent:
- 0:05ecf16dcb7b
- Child:
- 2:37d109555c95
Program to drive the servo in combination with the magnet grabber. Works without magnet or load attached to the servo. (see comments in code for more details on functionallity)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
teunman | 0:05ecf16dcb7b | 1 | #include "mbed.h" |
teunman | 1:26baa438620b | 2 | |
teunman | 1:26baa438620b | 3 | #include "Servo.h" |
teunman | 0:05ecf16dcb7b | 4 | |
teunman | 1:26baa438620b | 5 | |
teunman | 1:26baa438620b | 6 | DigitalOut green(LED2); //The led is on with the magnet |
teunman | 1:26baa438620b | 7 | DigitalOut magnet(D2); //magnet is defined on D2 |
teunman | 1:26baa438620b | 8 | DigitalIn knop(SW2); //The button used to cycle the entire pick up sequence |
teunman | 1:26baa438620b | 9 | |
teunman | 1:26baa438620b | 10 | |
teunman | 0:05ecf16dcb7b | 11 | int main() |
teunman | 0:05ecf16dcb7b | 12 | { |
teunman | 1:26baa438620b | 13 | |
teunman | 1:26baa438620b | 14 | Servo swagvo(D4); //devine the port for controlling the servo (this cannot be a PWM port !!!) |
teunman | 1:26baa438620b | 15 | int down = 0; //counter |
teunman | 1:26baa438620b | 16 | green = 1; |
teunman | 1:26baa438620b | 17 | |
teunman | 1:26baa438620b | 18 | swagvo.Enable(2350,20000); //turning on the servo (starting position,frequency (50 Hz do not change!!!)) |
teunman | 1:26baa438620b | 19 | // The servo has the following positions (fully left: 650, center (1500), fully right (2350)) these values prevent the servo from grinding so do not exceed them |
teunman | 1:26baa438620b | 20 | |
teunman | 1:26baa438620b | 21 | while(true) { |
teunman | 1:26baa438620b | 22 | |
teunman | 1:26baa438620b | 23 | |
teunman | 1:26baa438620b | 24 | |
teunman | 1:26baa438620b | 25 | |
teunman | 1:26baa438620b | 26 | |
teunman | 1:26baa438620b | 27 | if (knop == 0 and down == 0){ |
teunman | 1:26baa438620b | 28 | swagvo.SetPosition(650); //lower wire |
teunman | 1:26baa438620b | 29 | wait(1); |
teunman | 1:26baa438620b | 30 | green = 0; |
teunman | 1:26baa438620b | 31 | magnet = 1; //attach piece |
teunman | 1:26baa438620b | 32 | wait(1); |
teunman | 1:26baa438620b | 33 | swagvo.SetPosition(2350); //raise wire |
teunman | 1:26baa438620b | 34 | down = 1; |
teunman | 1:26baa438620b | 35 | } |
teunman | 1:26baa438620b | 36 | |
teunman | 1:26baa438620b | 37 | else if (knop == 0 and down == 1){ |
teunman | 1:26baa438620b | 38 | swagvo.SetPosition(650); //lower wire |
teunman | 1:26baa438620b | 39 | wait(1); |
teunman | 1:26baa438620b | 40 | green = 1; |
teunman | 1:26baa438620b | 41 | magnet = 0; //detach piece |
teunman | 1:26baa438620b | 42 | wait(1); |
teunman | 1:26baa438620b | 43 | swagvo.SetPosition(2350); //raise wire |
teunman | 1:26baa438620b | 44 | down = 0; //reset |
teunman | 1:26baa438620b | 45 | } |
teunman | 1:26baa438620b | 46 | |
teunman | 1:26baa438620b | 47 | } |
teunman | 1:26baa438620b | 48 | } |
teunman | 1:26baa438620b | 49 | |
teunman | 1:26baa438620b | 50 |