USB servo control

Dependencies:   Servo mbed

Files at this revision

API Documentation at this revision

Comitter:
faif
Date:
Sun Nov 13 12:22:13 2016 +0000
Commit message:
First version; ; Control a continuous rotation version using USB

Changed in this revision

Servo.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
usb-servo.cpp Show annotated file Show diff for this revision Revisions of this file
usb-servo.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 61aafea4a4fe Servo.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Sun Nov 13 12:22:13 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/simon/code/Servo/#36b69a7ced07
diff -r 000000000000 -r 61aafea4a4fe mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Nov 13 12:22:13 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/0ab6a29f35bf
\ No newline at end of file
diff -r 000000000000 -r 61aafea4a4fe usb-servo.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usb-servo.cpp	Sun Nov 13 12:22:13 2016 +0000
@@ -0,0 +1,82 @@
+#include "mbed.h"
+#include "Servo.h"
+#include "usb-servo.h" 
+
+
+static const char CLS[] = "\x1B[2J";        // VT100 erase screen
+static const char HOME[] = "\x1B[H";        // VT100 home
+
+static const float INITIAL_RANGE = 0.0005;
+static const float INITIAL_POSITION = 0.5;
+
+static const float CLOCK = 0.0;
+static const float CCLOCK = 1.0;
+static const float RANGE_MODIFIER = 0.0001;
+
+struct Movement
+{   
+    private:    
+        float calibration;
+        
+    public:
+        float range;
+        float position;
+     
+        Movement(float r, float p) : range(r), position(p)
+        {
+            calibration = 45.0;
+        };
+        
+        float getCalibration() const
+        {
+            return calibration;
+        }
+};
+ 
+int main() {
+    clear_screen();
+    show_menu();
+    Movement movement(INITIAL_RANGE, INITIAL_POSITION);
+    
+    while (true) 
+    {                  
+        switch(pc.getc()) 
+        {
+            case '1': 
+                movement.position = CLOCK;
+                break;
+            case '2': 
+                movement.position = CCLOCK;
+                break;
+            case '3': 
+                movement.range += RANGE_MODIFIER;
+                break; 
+            case '4': 
+                movement.range -= RANGE_MODIFIER;
+                break;
+        }
+        pc.printf("position = %.1f, range = +/-%0.4f\n\r", movement.position, movement.range);
+        update(myservo, movement);
+    }
+}
+
+void clear_screen()
+{
+    pc.printf(CLS);      
+    pc.printf(HOME);               
+}
+
+void show_menu()
+{
+    pc.printf("\rServo controls:\n\r");
+    pc.printf("1. Full speed clockwise\n\r");
+    pc.printf("2. Full speed counter-clockwise\n\r");
+    pc.printf("3. Increase speed\n\r");
+    pc.printf("4. Decrease speed\n\r");
+}
+
+void update(const Servo& servo, const Movement& move)
+{
+    myservo.calibrate(move.range, move.getCalibration()); 
+    myservo = move.position;    
+}
\ No newline at end of file
diff -r 000000000000 -r 61aafea4a4fe usb-servo.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usb-servo.h	Sun Nov 13 12:22:13 2016 +0000
@@ -0,0 +1,12 @@
+#ifndef USB_SERVO_H
+#define USB_SERVO_H
+
+Servo myservo(p21);
+Serial pc(USBTX, USBRX);
+struct Movement;
+
+void clear_screen();
+void show_menu();
+void update(const Servo& servo, const Movement& move);
+
+#endif