Switches between A en B bij pressing a button

Dependencies:   MODSERIAL Motordriver mbed

main.cpp

Committer:
meikefrok
Date:
2016-10-14
Revision:
0:60a905a48133

File content as of revision 0:60a905a48133:

#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);
}