Switches between A en B bij pressing a button

Dependencies:   MODSERIAL Motordriver mbed

Files at this revision

API Documentation at this revision

Comitter:
meikefrok
Date:
Fri Oct 14 13:18:28 2016 +0000
Commit message:
Add motor;

Changed in this revision

MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
Motordriver.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp 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
diff -r 000000000000 -r 60a905a48133 MODSERIAL.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODSERIAL.lib	Fri Oct 14 13:18:28 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Sissors/code/MODSERIAL/#4737f8a5b018
diff -r 000000000000 -r 60a905a48133 Motordriver.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motordriver.lib	Fri Oct 14 13:18:28 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/littlexc/code/Motordriver/#3110b9209d3c
diff -r 000000000000 -r 60a905a48133 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Oct 14 13:18:28 2016 +0000
@@ -0,0 +1,126 @@
+#include "mbed.h"
+#define SERIAL_BAUD 115200  // baud rate for serial communication
+#include "MODSERIAL.h"
+#include "motordriver.h"
+MODSERIAL pc(USBTX,USBRX);
+
+// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = >
+// Timing
+const float kTimeLedToggle = 0.25f; // period with which to toggle LED
+const float kTimePrintSerial = 1.0f;// period with which data is printed
+
+// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = >
+// constants
+const int kNumLeds = 3;             // three LEDs
+const int kLedOn = 0;               // LED on if 0
+
+// LEDs
+DigitalOut led_0(LED_RED);
+DigitalOut led_1(LED_GREEN);
+
+// ID of led that should blink; *volatile* because changed by interrupt
+volatile int led_blink_id = 0;
+
+// counters
+int num_turned_on_0 = 0;            // count number of times red LED turned on
+int num_turned_on_1 = 0;            // count number of times green LED turned on
+
+//Safety for Motor
+int Brakeable; //cna the motor driver break
+int sign; //prevents throwing the motor from full foward to full reverse and stuff melting.
+
+
+//Motor
+Motor A(D6, D7, D7, Brakeable); // pwm, fwd, rev, brake
+Motor B(D5, D4, D4, Brakeable); // pwm, fwd, rev, brake
+
+/**
+ * Toggle / Switch / Blink the state of the LED.
+ * Count the number of times the LED was turned ON.
+ * We have quite some duplicate code here; this is not ideal and could be solved
+ * using more 'advanced' concepts such as arrays and enums. 
+ * However, this is outside the scope of this course for now.
+ */
+void ToggleLed()
+{
+    switch (led_blink_id) {
+        case 0: {
+            // LED 0 to toggle and count:
+            led_0 = not led_0;
+            A.speed(0.5) == 0.5;               //If speed is 0, the motor doensn't rotate.
+            B.speed(0) == 0;
+            if (led_0 == kLedOn) {
+                num_turned_on_0++;
+            }
+            
+            // LEDs to turn off:
+            led_1 = not kLedOn;
+            
+            break;
+        }
+        case 1: {
+            // LED 1 to toggle and count:
+            led_1 = not led_1;
+            A.speed(0) == 0;               //If speed is 0, the motor doensn't rotate.
+            B.speed(0.5) == 0.5;
+            if (led_1 == kLedOn) {
+                num_turned_on_1++;
+            }
+            
+            // LEDs to turn off:
+            led_0 = not kLedOn;
+            
+            break;
+        }
+    }
+}
+
+/**
+ * Print the number of times each LED was turned on through serial communication
+ */
+void PrintSerial()
+{
+    pc.printf("*Status*\r\n\tn_r = %d\r\n\tn_g = %d\r\n\tn_b = %d\r\n\r\n",
+        num_turned_on_0, num_turned_on_1);
+}
+
+/**
+ * Switch the led id that blinks 
+ * led_blink_id goes from 0 -> 1 -> 2 -> 0 -> ...
+ * @ensure led_blink_id = ++led_blink_id % kNumStates
+ */
+void SwitchLedColor() {
+    led_blink_id++;
+    if (led_blink_id>=kNumLeds) {
+        // could also use modulo ('%') operator
+        led_blink_id = 0;
+    } 
+}
+
+/**
+ * Main loop.
+ */
+int main()
+{
+    // Serial comm baud rate
+    pc.baud(SERIAL_BAUD);
+    pc.printf("\r\n**RESET**\r\n");
+    
+    // Turn off all LEDs initially
+    led_0 = not kLedOn;
+    led_1 = not kLedOn;
+    
+    // Create ticker and attach LED toggle function
+    Ticker tick_toggle_led;
+    tick_toggle_led.attach(&ToggleLed,kTimeLedToggle);
+    
+    // Create ticker and attach Print function
+    Ticker tick_print_serial;
+    tick_print_serial.attach(&PrintSerial,kTimePrintSerial);
+    
+    // Create interrupt and attach switch function
+    InterruptIn sw2(SW2);
+    sw2.fall(&SwitchLedColor);
+    
+    while (true);
+}
diff -r 000000000000 -r 60a905a48133 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Oct 14 13:18:28 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/25aea2a3f4e3
\ No newline at end of file