lololololol

Dependencies:   mbed QEI HIDScope biquadFilter MODSERIAL FastPWM

main.cpp

Committer:
Hendrikvg
Date:
2019-10-04
Revision:
19:5ac8b7af77a3
Parent:
18:50c04dd523ea
Child:
20:ac1b4ffa3323

File content as of revision 19:5ac8b7af77a3:

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

// ENC1A --> D13
// ENC1B --> D12
// POT1 --> A0
// LED1 --> D3
// BUT1 --> D1
// BUT2 --> D0

MODSERIAL pc(USBTX, USBRX);
DigitalOut ledr(LED_RED);
DigitalOut ledg(LED_GREEN);
DigitalOut ledb(LED_BLUE);
Ticker ReduceEmission;
Timer R;
Timer G;
Timer B;

FastPWM lichtje(D3);
AnalogIn   ain(A0);
DigitalOut PWM_motor_1(D6);
DigitalOut direction_motor_1(D7);

HIDScope scope(2);
QEI encoder(D12,D13,NC,64,QEI::X4_ENCODING);
Ticker RW_scope;

// variables

volatile char command = 'r';
enum states {red, green, blue};
states CurrentState = red;
bool StateChanged = true;

volatile int on_time_ms; // The time the LED should be on, in microseconds
volatile int off_time_ms;

int degrees;
volatile double x;
volatile double x_prev=0;
volatile double y;

// functions

void TurnLedRed(){
    ledr = 0;
    ledg = 1;
    ledb = 1;}

void TurnLedGreen(){
    ledr = 1;
    ledg = 0;
    ledb = 1;}

void TurnLedBlue(){
    ledr = 1;
    ledg = 1;
    ledb = 0;}

void PulseWidthModulation() // Bepaalt de duty cycle gebaseerd op de potentiometer en zet de motor dan op die snelheid mbh PWM
{   on_time_ms = (int) (ain.read()*(1/1e2)*1e3);
    off_time_ms = (int) ((1-ain.read())*(1/1e2)*1e3);
    PWM_motor_1 = 1;
    wait_ms(on_time_ms);
    PWM_motor_1 = 0;
    wait_ms(off_time_ms);}

void EnergySaving() // Functie voor de "ReduceEmissions" ticker, zet de motor uit en LED op rood
{    command = 'r';}

void CheckCommandFromTerminal(void) // Functie voor de in de main loop
{    command = pc.getcNb();}

void WhileRed()
{    if (command == 'g') {
        R.stop();
        pc.printf("The LED has been red for %f seconds!\n\r", R.read());
        CurrentState= green;
        StateChanged= true;    }
    if (command == 'b') {
        R.stop();
        pc.printf("The LED has been red for %f seconds!\n\r", R.read());
        CurrentState= blue;
        StateChanged= true;    }}

void WhileGreen()
{   PulseWidthModulation();
    if (command == 'r') {
        G.stop();
        pc.printf("The LED has been green for %f seconds!\n\r", G.read());
        CurrentState= red;
        StateChanged= true;    }
    if (command == 'b') {
        G.stop();
        pc.printf("The LED has been green for %f seconds!\n\r", G.read());
        CurrentState= blue;
        StateChanged= true;    }}

void WhileBlue()
{   PulseWidthModulation();
    if (command == 'r') {
        B.stop();
        pc.printf("The LED has been blue for %f seconds!\n\r", B.read());
        CurrentState= red;
        StateChanged= true;    }
    if (command == 'g') {
        B.stop();
        pc.printf("The LED has been blue for %f seconds!\n\r", B.read());
        CurrentState= green;
        StateChanged= true;    }}

void ReadEncoderAndWriteScope(){
    degrees = ((360/64)*encoder.getPulses())/131.25; // 360/64 voor de 64 CPR Encoder, 131.25 omdat de gear ratio 131.25:1 is. Hiermee is 1 omwenteling 360.
    x = degrees;
    scope.set(0,x);
    y = (x_prev + x)/2.0;
    scope.set(1,y);
    x_prev=x;
    scope.send();}

void StateMachine(void)
{
    switch(CurrentState) {
        case red:
            if  (StateChanged) {
                pc.printf("Initiating turning LED red\n\r");
                StateChanged= false;
                TurnLedRed();
                R.start();
                PWM_motor_1 = 0;
                pc.printf("LED is now red!\n\r");
            }
            WhileRed();
            break;
        case green:
            if  (StateChanged) {
                pc.printf("Initiating turning LED green\n\r");
                StateChanged= false;
                TurnLedGreen();
                G.start();
                direction_motor_1 = 0;
                pc.printf("LED is now green!\n\r");
            }
            WhileGreen();
            break;
        case blue:
            if  (StateChanged) {
                pc.printf("Initiating turning LED blue\n\r");
                StateChanged= false;
                TurnLedBlue();
                B.start();
                direction_motor_1 = 255;
                pc.printf("LED is now blue!\n\r");
            }
            WhileBlue();
            break;
        default:
            pc.printf("Unknown or unimplemented state reached!\n\r");
    }
}

// main

int main()
{
    pc.baud(115200);
    pc.printf("Hello World!\n\r");
    RW_scope.attach(&ReadEncoderAndWriteScope, 0.1);
    ReduceEmission.attach(EnergySaving,20);
    while(true) {
        CheckCommandFromTerminal();
        StateMachine();
        lichtje.write(ain.read());  // duty cycle
    }
}