new battery bar

Dependencies:   CAN_IDs CanControl Dashboard PinDetect PowerControl mbed-rtos mbed

main.cpp

Committer:
zathorix
Date:
2017-05-22
Revision:
13:8e92260c63bd
Parent:
11:005a50dd7db5

File content as of revision 13:8e92260c63bd:

/*******************************************************************************
This software is especially designed for Solarboat Twente for the use in their Solarboat v1.0

Written by:
Niels Leijen, Jesse van Rhijn, Bram Seinhorst

Thanks to:
Jasper Admiraal, Hidde Pik (hihihihi), Lisa Postma, Heleen Jeurink, Ruben Peters,
Martijn Groot Jebbink, Martijn Wilpshaar, Bram Seinhorst, Robert Geels, Arnoud Meutstege,
Jeroen te Braake, Ids de Vos, Jesse van Rhijn, Sam Benou, Niels Leijen and Mark Bruijn

DISCLAIMER:
THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. 
SOLARBOATTWENTE ASSUMES NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE.
*******************************************************************************/

// uncomment to send debug information
#define DEBUG

//Speed difference threshold to prevent CAN spam from throttle
#define SPEED_THRESH 0.05

//include 3rd party libraries
#include "mbed.h" //needs to be revision 136 else SD filesystem will not work
#include "rtos.h"

// include Solarboat libraries
#include "pinout.h"
#include "PowerControl.h"
#include "CAN_IDs.h"

// initialize serial connection for debug
#ifdef DEBUG
RawSerial pc(SERIAL_TX, SERIAL_RX);
#endif

//initialize throttle 
AnalogIn analog_throttle(STEER_THROTTLE);

// initialize canbus
CAN can(CAN_RD, CAN_TD);

// initialze onboard leds
DigitalOut ledError(LED3);
DigitalOut ledSD(LED1);
DigitalOut ledFona(LED5);
DigitalOut led24V(LED4);

//DigitalOut buckCan(BUCK2);
DigitalOut buckXSens(BUCK3);
DigitalOut buckScreen(BUCK4);
DigitalOut buck24V(BUCK5);

//Motor functions
void sendMotorSpeed(float throttle);
void readThrottle();

// Thread 1 - Power
void power(){
    PowerControl powercontrol(PUSH_GREEN); 
    
#ifdef DEBUG
    pc.printf("Thread 1 - Power started\r\n ");
#endif

    Thread::wait(osWaitForever);
}

// Thread X - Test
void test(){
    ledError = 0;
    ledSD = 0;
    ledFona = 0 ;
    led24V=1;

//DigitalOut buckCan(BUCK2);
    buckXSens = 0;
    buckScreen = 0;
    buck24V = 1;
}

// Thread 0 - DO NOT CHANGE THIS!
int main() {  
#ifdef DEBUG
    pc.baud(115200);
    pc.printf("Starting SOS V1.0\n");
#endif
    // change CAN frequency
    can.frequency(250000);
    
    // initialze threads
    Thread thread1;
    //Thread thread2;
    //Thread thread3;
    //Thread thread4;
    Thread threadx;
    Thread threadthrottle;
    
    // change thread priority
    //thread2.set_priority(osPriorityBelowNormal);
    
    // start threads
    thread1.start(&power);
    //thread2.start(&calcPi);
    //thread3.start(&motorTest);
    //thread4.start(&canReceive);
    threadx.start(&test);
    threadthrottle.start(&readThrottle);
    
    //stop this thread while keeping the other threads running
    CANMessage msg;
    while(1) {
        if(can.read(msg)) {
            pc.printf("Message received: 0x %x; %u %u%u %d%d %c %u %u\n", msg.id, msg.data[0], msg.data[1], msg.data[2], msg.data[3], msg.data[4], msg.data[5], msg.data[6], msg.data[7]);
        } 
    }
    Thread::wait(osWaitForever);
}

//send motor command
void sendMotorSpeed(float throttle) {
    if ((abs(throttle - speed)) > SPEED_THRESH) {
        union {
            char msg[4];
            float value;
        } packet;
        packet.value = throttle;
        can.write(CANMessage(MOTOR_COMMAND, packet.msg));
        printf("Sent motor speed: %f\r\n", packet.value);
        speed = throttle;
    }
}

//throttle thread
void readThrottle() {
    while(1) {
        throttleread = 2*(0.5 - analog_throttle.read());
        sendMotorSpeed(rev*throttleread);
        Thread::wait(100);   
    }
}