ELCT 302 / Mbed 2 deprecated Driving

Dependencies:   mbed

Fork of Driving by Kyle Drain

main.cpp

Committer:
KDrainEE
Date:
2018-03-29
Revision:
1:c6d269c69853
Parent:
0:8c9ee304ed9f
Child:
2:1fd3a4aff5d3

File content as of revision 1:c6d269c69853:

#include "mbed.h"
#include <iostream>

using namespace std;

#define SCALAR 0.6f
#define TACH PTB1
#define SPEED_CONTROL PTD5
#define BRAKE PTA13

#define TI 0.001f
#define MINm 0.0f
#define MAXm 1.0f
#define KPm 0.1414f
#define KI 19.7408f

Ticker sample;
AnalogIn speed(TACH);
PwmOut gateDrive(SPEED_CONTROL);
DigitalOut brake(BRAKE);

float Setpoint;
float errSum;

Serial bt(PTE0, PTE1); //COM12

void serCb()
{
    char x = bt.getc();
    if (x == 'a')
    {
        Setpoint = 0.25;
    }
    else if(x == 's')
    {
        Setpoint = 0.6;
    }
    else
    {
        bt.putc(x);
    } 
}

void compute_PI()
{
    float error = Setpoint-speed.read();
    errSum +=(error * TI);
    float iTerm = KI*errSum;
    if(iTerm > MAXm) iTerm = MAXm;
    if(iTerm < MINm) iTerm = MINm; 
    float output = KPm*error + iTerm;
    if(output > MAXm) output = MAXm;
    if(output < MINm) output = MINm;    
    if(output < MINm) 
    {
        gateDrive.write(MINm);
        brake.write(1);
    }
    else 
    {
        brake.write(0);
        gateDrive.write(output);
    }           
}

int main()
{ 
    Setpoint = 0.0;
    errSum = 0.0;
    sample.attach(&compute_PI, TI);
   
    bt.baud(115200);
    bt.printf("Press 'a' to go 25 and 's' to go 60%\r\n");
    bt.attach(&serCb);
     
    while(true)
    {

    }  
}