![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Test servo using the serial port.
Dependencies: mbed
TFC_servo_test.cpp@0:4abb1d756ad6, 2014-10-12 (annotated)
- Committer:
- jmar11
- Date:
- Sun Oct 12 17:21:05 2014 +0000
- Revision:
- 0:4abb1d756ad6
First commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmar11 | 0:4abb1d756ad6 | 1 | #include "mbed.h" |
jmar11 | 0:4abb1d756ad6 | 2 | #include "TFC.h" |
jmar11 | 0:4abb1d756ad6 | 3 | |
jmar11 | 0:4abb1d756ad6 | 4 | bool isValid(float); |
jmar11 | 0:4abb1d756ad6 | 5 | |
jmar11 | 0:4abb1d756ad6 | 6 | Serial pc(USBTX, USBRX); |
jmar11 | 0:4abb1d756ad6 | 7 | |
jmar11 | 0:4abb1d756ad6 | 8 | int main(){ |
jmar11 | 0:4abb1d756ad6 | 9 | float pos = 0; |
jmar11 | 0:4abb1d756ad6 | 10 | int deg = 0; |
jmar11 | 0:4abb1d756ad6 | 11 | uint8_t servo = 0; |
jmar11 | 0:4abb1d756ad6 | 12 | |
jmar11 | 0:4abb1d756ad6 | 13 | TFC_Init(); |
jmar11 | 0:4abb1d756ad6 | 14 | |
jmar11 | 0:4abb1d756ad6 | 15 | pc.printf("Please enter a posiiton for your servo (-1.0-1.0)\r\n"); |
jmar11 | 0:4abb1d756ad6 | 16 | |
jmar11 | 0:4abb1d756ad6 | 17 | while(1){ |
jmar11 | 0:4abb1d756ad6 | 18 | pc.scanf("%f", &pos); |
jmar11 | 0:4abb1d756ad6 | 19 | |
jmar11 | 0:4abb1d756ad6 | 20 | if(isValid(pos)){ |
jmar11 | 0:4abb1d756ad6 | 21 | deg = pos * 90; |
jmar11 | 0:4abb1d756ad6 | 22 | pc.printf("Your servo is at position %1.3f or %2i degrees \r", pos, deg); |
jmar11 | 0:4abb1d756ad6 | 23 | TFC_SetServo(servo, pos); |
jmar11 | 0:4abb1d756ad6 | 24 | } |
jmar11 | 0:4abb1d756ad6 | 25 | else{ |
jmar11 | 0:4abb1d756ad6 | 26 | pc.printf("Sorry, invalid command. \r"); |
jmar11 | 0:4abb1d756ad6 | 27 | pos = 0; |
jmar11 | 0:4abb1d756ad6 | 28 | TFC_SetServo(servo, pos); |
jmar11 | 0:4abb1d756ad6 | 29 | } |
jmar11 | 0:4abb1d756ad6 | 30 | wait_ms(500); |
jmar11 | 0:4abb1d756ad6 | 31 | } |
jmar11 | 0:4abb1d756ad6 | 32 | } |
jmar11 | 0:4abb1d756ad6 | 33 | |
jmar11 | 0:4abb1d756ad6 | 34 | bool isValid(float pos){\ |
jmar11 | 0:4abb1d756ad6 | 35 | if (pos <= 1.0f && pos >= -1.0f) |
jmar11 | 0:4abb1d756ad6 | 36 | return true; |
jmar11 | 0:4abb1d756ad6 | 37 | else |
jmar11 | 0:4abb1d756ad6 | 38 | return false; |
jmar11 | 0:4abb1d756ad6 | 39 | } |