All tasks complete

Dependencies:   mbed MCP23017 mbed-rtos WattBob_TextLCD

assignment3tasks.cpp

Committer:
aingks
Date:
2019-03-27
Revision:
9:2fd97246b8f0
Parent:
8:3f3f2c2e2046
Child:
10:2b262d810c67

File content as of revision 9:2fd97246b8f0:

#include "header.h"


//definitions of tasks
//lock and unlock variables in struct when performing calculations 

//pedals 
AnalogIn            brake_pedal(pin_analog_1);
AnalogIn            accel_pedal(pin_analog_2);


//switches
DigitalIn           ngene(pin_digital_1);
DigitalIn           leftsw(pin_digital_2);
DigitalIn           rightsw(pin_digital_3);
DigitalIn           sidesw(pin_digital_4);


//indicators 
DigitalOut          ngeneind(pin_LED1);
PwmOut              leftind(pin_LED2); //was digitalout
PwmOut              rightind(pin_LED3);//was digitalout
DigitalOut          sideind(pin_LED4);


//RED box lights
DigitalOut          brakeind(pin_LED5);
DigitalOut          overspeedind(pin_LED6);



//
extern SDATA speed;
extern CARINFO info;
extern SIMU_DATA sim;

// LCD

extern WattBob_TextLCD *display;

// MUTEX

extern Mutex simuXS;
extern Mutex speedXS;
extern Mutex carstateXS;
//


void task1readbrake()
{  
   float delay = 1000 / TASKFREQ1;
   while(true){
       
   speedXS.lock();//lock
   
   speed.brakevalue = brake_pedal.read(); 
   
   speedXS.unlock();//unlock
   
   Thread::wait( (int) delay);
   
   }
}

void task2readaccel()
{
   float delay = 1000 / TASKFREQ2;
   while(true){
       
   speedXS.lock();//lock
   
   speed.accelvalue = accel_pedal.read(); 
   
   speedXS.unlock();//unlock
   
   Thread::wait( (int) delay);
   
   }
   
}

void task3enginestate()
{
    float delay = 125;      
    while(true){
    //lock
    carstateXS.lock();
    uint8_t engineonoff = ngene.read();  
    info.ENGINESTATE = engineonoff;
    carstateXS.unlock();
    //unlock
    
    if(engineonoff == 1){
        ngeneind = 1;
        }
    else{
        ngeneind = 0;
        }
        
    Thread::wait(125);
    }             
}

void task4average()
{
    float delay = 1000 / TASKFREQ4;    
    while(true){
    speedXS.lock();//lock
    
    for(int i = 0; i < SAMPLESIZE ; i++){
        //speed.rawspeed = (speed.accelvalue - speed.brakevalue)*MAXSPEED;
        speed.average += speed.array[i];
        }
        speed.average = (speed.average / SAMPLESIZE);    
               
    speedXS.unlock();//unlock 
    
    Thread::wait( (int) delay);
    }
}

void task5brakeLED()
{
    float delay = 1000 / TASKFREQ5;
    while(true){
        
        //lock
        speedXS.lock();
        float braketest = speed.brakevalue;     
        
        speedXS.unlock();//unlock
        
        if( braketest > 0.2) // speed increase
            brakeind = 1;
        else
            brakeind = 0;
    
    Thread::wait((int)delay);
        }
}


void task6speedmonitor()
{
    float delay = 1000 / TASKFREQ6;
    while(true){
        
        carstateXS.lock();  //lock carstate
        speedXS.lock();     //lock speed
        
        
        info.odometer += (speed.rawspeed / TASKFREQ6);
        uint8_t temp_enginestate = info.ENGINESTATE;
        
        carstateXS.unlock(); //unlock carstate
        
        float temp_accel = 0.0;
        if(temp_enginestate != 0)
            temp_accel = speed.accelvalue;
        
        //update speed    
        speed.rawspeed += (temp_accel - speed.brakevalue) * MAXSPEED; 
        
        if(speed.rawspeed < 0)
        speed.rawspeed = 0;
        if(speed.rawspeed > MAXSPEED)
        speed.rawspeed = MAXSPEED;   
        
        // Saving the new speed
        for (int i = 0; i < SAMPLESIZE - 1; i++)
        
        speed.array[i]= speed.array[i+1]; 
        speed.array[4]= speed.rawspeed;
            
        float temp = speed.rawspeed;
        
        // UNLOCK Speed
        speedXS.unlock();
        
        // Update Overspeed display
        
        if (temp > SPEEDLIMIT)
            overspeedind = 1;
        else
            overspeedind = 0;
            
        Thread::wait((int)delay);
        }
}

void task7displayvalues()
{
    float delay = 1000 / TASKFREQ7;
    while(true){
        
        display->cls();
    
        //LOCK data
        carstateXS.lock();
        //LOCK Speed
        speedXS.lock();
        
        display->locate(1,0);
        display->printf("AvgS: %.2f",speed.average);
        display->locate(0,0);
        display->printf("ODOM: %d",(int) info.odometer);
     
        //UNLOCK data
        carstateXS.unlock();
        //UNLOCK Speed
        speedXS.unlock();
        


        
        Thread::wait(500);
        
        
        }
}

void task8sidelights()
{
    float delay = 1000 / TASKFREQ8;
    while(true){
               
        // LOCK Car data
        carstateXS.lock();
        
        info.sideind = sidesw.read();
        uint8_t temp = info.sideind;
        
        //UNLOCK Car data
        carstateXS.unlock();
        
        if (temp == 1)
            sideind = 1;
        else
            sideind = 0;
        
        Thread::wait((int)delay);
        
        
        
        
        }
}

void task9indLED()
{
    float delay = 1000 / TASKFREQ9;
    while(true){
        uint8_t turnleft = leftsw.read();
        uint8_t turnright = rightsw.read();
        
        carstateXS.lock();
        
        

       (turnleft = 1)?     info.leftind    =   1   :info.leftind   =    0;
       (turnright = 1)?    info.rightind   =   1   :info.rightind  =    0; 
        
       /* if(turnleft = 1){
          info.leftind = 1;
          }
        else{
          info.leftind = 0;
          }
          
        if(turnleft = 1){
          info.rightind = 1;
          }
        else{
          info.rightind = 0;
          }
             */
          
        carstateXS.unlock();
        
        //hazard and other lights
        
        float left_period = 1.0;
        float right_period = 1.0;
        float left_pulswidth = 0.0;
        float right_pulswidth = 0.0;

        if (turnleft != 0)
        {
            left_period = (1/TASKFREQ9);
            left_pulswidth = (1/(2*TASKFREQ9)); 
        }
        if (turnright!= 0)
        {
            right_period = (1/TASKFREQ9);
            right_pulswidth = (1/TASKFREQ9);
        }
        if (turnleft != 0 && turnright != 0)
        {
            left_period = left_period / 2;
            left_pulswidth = left_pulswidth / 2;
            right_period = right_period / 2;
            right_pulswidth = right_pulswidth / 2;   
        }
        
        leftind.period(left_period);
        leftind.pulsewidth(left_pulswidth);
        rightind.period(right_period);
        rightind.pulsewidth(right_pulswidth);
       
       float delay = 1000 / TASKFREQ9S2;
       Thread::wait((int)delay);
        
        
        
        }




}