Ticker added

Dependencies:   HIDScope MODSERIAL QEI mbed

Fork of Tau_new_ticker_toevoegen by V D

main.cpp

Committer:
ARGroenenboom
Date:
2017-11-02
Revision:
8:62c7083f12d9
Parent:
7:05bef8a64a44
Child:
9:23fa7b03e2fe

File content as of revision 8:62c7083f12d9:

#include "mbed.h"
#include "HIDScope.h"
#include "QEI.h"
#include "iostream"
#include "MODSERIAL.h"

//Leds
DigitalOut ledb(LED_BLUE);
DigitalOut ledr(LED_RED);
DigitalOut ledg(LED_GREEN);

//Motor
DigitalOut motor1DC(D7);
PwmOut motor1PWM(D6);

//Button
DigitalIn   button1(SW2);

// HIDscope
HIDScope scope(1);

//Ticker
Ticker tick;
float f = 0.2;
int counter=0;

// Encoder
float X = 32.0;
float N = 131.0;
float pulse_count;
float angle_motor;
QEI Encoder(D8,D9,NC,N);
MODSERIAL pc(USBTX,USBRX);

// Send angle to HIDScope and pc function
void Calculate()
{
    // Get pulses and calculate angle
    pulse_count = Encoder.getPulses();
    float angle_pp =360/(X * N);
    float angle = pulse_count*angle_pp;

    // Send angle to HIDScope and pc
    scope.set(0, angle);
    scope.send();
    pc.printf(" pulse count: %f angle: %f \r \n ", pulse_count, angle);
    
    counter++;
    
    // Turn motor off and reset encoder to reset pules count
    
    if(counter==15)
    {
    motor1PWM = 0; // motor off
    Encoder.reset();
    counter = 0;
    
    // Show ready state through leds
    wait(1.0); // wait before next roll to protect motors
    ledr = 1; // red led off
    ledb = 0; // blue led on = finished measuring
    ledg = 1; // green led ff
    
    }
    
    //if(button1==false)
    //{
    //    Roll();
    //}   
}

void Measure()
{
    tick.attach(&Calculate, f);
}

// Roll function
void Roll()
{ 
    // Show rolling state through leds
    ledg = 1; // green led off
    ledb = 1; // blue led off
    ledr = 0; // red led on = rolling

    // Turn motor on for x time and turn it off again
    motor1PWM = 1; // motor on
    Measure();
    
    
    //wait(3.00); // Let motor turn for x time    
}


int main()
{
    // Initialize system
    pc.baud(9600);
    motor1DC = -1;
    ledb = 1;
    ledr = 1;
    ledg = 0;
    
    // Dice code
    if(button1==false) 
    {
        Roll();
    }
    

    // Perform main continuously
    while(true) {}
}

// screen /dev/tty.usbmodem1412 9600