Plz

Dependencies:   mbed CANMsg Adafruit_LEDBackpack

main.cpp

Committer:
fconboy
Date:
2019-08-05
Revision:
7:060be032c57a
Parent:
6:be28839a8221
Child:
8:3eb4fcab8ede

File content as of revision 7:060be032c57a:

#include "mbed.h"
#include "CANMsg.h"
#include "Adafruit_LEDBackpack.h"

Ticker ticker;
Timer timer;
AnalogIn currentPot(p15);
float curr_val = 0;
float curr_reading = 0;
AnalogIn speedPot(p16);
float speed_val = 0;
float speed_reading = 0;
float speed_reading_ms = 0;
float speed_reading_kmh = 0;
DigitalIn ignition(p21);
DigitalIn regen(p22);
DigitalIn rev(p23);
DigitalIn brake(p24);
DigitalIn accel(p25);
float maxBusCurrent = 1.0;

I2C i2c(p28,p27);
Adafruit_LEDBackpack display(&i2c);

DigitalOut debug1(LED1);
DigitalOut debug2(LED2);
DigitalOut debug3(LED3);
DigitalOut debug4(LED4);

//CAN can1(p9, p10);
CAN can1(p30, p29);

const uint16_t SIGNAL_ID = 0x501;
const uint16_t BUS_ID = 0x502;
const uint8_t DISPLAY_ADDR = 0x70; 
char counter = 0;
Serial pc(USBTX, USBRX); // tx, rx

CANMsg driverControls;
CANMsg busCurrent;

//====================================================================

void printMsg(CANMessage& msg)
{
    pc.printf("-------------------------------------\r\n");
    pc.printf("  ID      = 0x%.3x\r\n", msg.id);
    pc.printf("  Type    = %d\r\n", msg.type);
    pc.printf("  Format  = %d\r\n", msg.format);
    pc.printf("  Length  = %d\r\n", msg.len);
    pc.printf("  Data    =");
    pc.printf("-------------------------------------\r\n");
    for(int i = 0; i < msg.len; i++)
        pc.printf(" 0x%.2X", msg.data[i]);
    pc.printf("\r\n");
}

//=====================================================================
/*
void onCanReceived(CANMessage& msg)
{
    can1.read(msg);
    pc.printf("-------------------------------------\r\n");
    pc.printf("CAN message received\r\n");
    pc.printf("-------------------------------------\r\n");
    printMsg(msg);
 
    if (msg.id == MY_ID) {
        // extract data from the received CAN message 
        // in the same order as it was added on the transmitter side
        msg >> counter;
        msg >> voltage;    
        pc.printf("  counter = %d\r\n", counter);
        pc.printf("  voltage = %e V\r\n", voltage);
    }
    timer.start(); // to transmit next message in main
}
*/
//======================================================================

void flash(int light)   //Turns on one of the 4 mbed LEDs
{   if (light == 1)
    {debug1 = 1;
    wait(0.2);
    debug1 = 0;}
    else if (light == 2)
    {debug2 = 1;
    wait(0.2);
    debug2 = 0;}
    else if (light == 3)
    {debug3 = 1;
    wait(0.2);
    debug3 = 0;}
    else if (light == 4)
    {debug4 = 1;
    wait(0.2);
    debug4 = 0;}
    else if (light == 5)
    {debug1 = 1;
    debug2 = 1;
    debug3 = 1;
    debug4 = 1;
    wait(0.2);
    debug1 = 0;
    debug2 = 0;
    debug3 = 0;
    debug4 = 0;}}

void setDriverControls()
{
    driverControls.clear();
    driverControls.id = SIGNAL_ID;
    driverControls << speed_val;
    driverControls << curr_val;
    
    busCurrent.clear();
    busCurrent.id = BUS_ID;
    busCurrent << 0;
    busCurrent << maxBusCurrent;
}

void sendCAN()
{
    setDriverControls();
    //printMsg(driverControls);
    //printMsg(busCurrent);
    if(can1.write(driverControls))
    {
        debug1 = !debug1;
    }
    else
    {
        //wait(0.5);
        //flash(3);
    }
    
    //if(can1.write(busCurrent))
    //{
    //    debug2 = !debug2;
    //}
    //else
    //{
        //wait(0.5);
    //}
}

int main()
{

    pc.baud(9600);
    can1.frequency(1000000);
    ticker.attach(&sendCAN, 1);
    ignition.mode(PullUp);
    regen.mode(PullUp);
    rev.mode(PullUp);
    brake.mode(PullUp);
    accel.mode(PullUp);
    
    
//    display.begin(DISPLAY_ADDR);
//    for (int n = 0; n<16; n++)
//    {
//    display.setBrightness(n);
//    for (int i = 0; i<10; i++)
//    {
//    display.displaybuffer[0] = i;
//    display.displaybuffer[1] = i;
//    display.displaybuffer[2] = 0;
//    display.displaybuffer[3] = 0;
//    display.displaybuffer[4] = 0;
//    display.displaybuffer[5] = 0;
//    display.displaybuffer[6] = 0;
//    display.displaybuffer[7] = 0;
//    display.writeDisplay();
//    wait(0.5);
//    }
//    }
    
    
    //pc.printf("-------------------------------------\r\n");
    //printf("Attempting to send a CAN message\n");
    //printf("Current and Speed: %f and %f\n", currentPot.read(), speedPot.read());
    //pc.printf("-------------------------------------\r\n");
    //while(1) {
        
        //curr_val = 0.0;
        //speed_val = 0.0;
        curr_reading = currentPot.read();
        speed_reading = speedPot.read();
        speed_reading_ms = speed_reading * 30;
        speed_reading_kmh = speed_reading_ms*3.6;
        
        bool ignition_reading = ignition.read();
        bool regen_reading = regen.read();
        bool rev_reading = rev.read();
        bool brake_reading = brake.read();
        bool accel_reading = accel.read();
        
        //pc.printf("Current reading: %f\r\n", curr_reading);
        //pc.printf("Speed reading: %f\r\n", speed_reading);
        //pc.printf("Ignition reading: %d\r\n", ignition_reading);
        //pc.printf("Regen reading: %d\r\n", regen_reading);
        //pc.printf("Forward/reverse reading: %d\r\n", rev_reading);
        //pc.printf("Brake reading: %d\r\n", brake_reading);
        //pc.printf("Accelerator reading: %d\r\n\r\n", accel_reading);
        //wait(2);
        
        if (ignition)
        {
            curr_val = 0;
            speed_val = 0;
        }
        else
        {
            if (!brake && !regen)
            {
                speed_val = 0;
                curr_val = curr_reading;
            }
            else if(!brake && regen)
            {
                speed_val = 0;
                curr_val = 0;
            }
            else
            {
                if (!rev)
                {
                    speed_val = (-1)*speed_reading_ms;
                }
                else
                {
                    speed_val = speed_reading_ms;
                }
                
                if (!accel)
                {
                    curr_val = curr_reading;
                }
                else
                {
                    curr_val = 0;
                }
            }
        }
        //pc.printf("speed_val: %f\r\n", speed_val);
        //pc.printf("curr_val: %f\r\n\r\n", curr_val);
        //sendCAN();
        //pc.printf("\r\n\r\n");
        //wait(3);
    //}
}