CE201 Embedded : 2022-23 / Mbed 2 deprecated Car

Dependencies:   mbed

main.cpp

Committer:
fjwats
Date:
2017-03-02
Revision:
5:66527172b136
Parent:
4:54544a7dcbe0
Child:
6:54991513b762

File content as of revision 5:66527172b136:

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

#define MAX 0x0A

DigitalOut led_1(LED1);         // program running.
DigitalOut led_2(LED2);         // sensors operating.
DigitalOut led_3(LED3);         // is moving.
DigitalOut led_4(LED4);         // is complete.

PwmOut steer(p21);              // Pulse width modulation pins that control direction
PwmOut speed(p22);              // and speed

DigitalOut Bit1(p25);           // multiplexor pins
DigitalOut Bit2(p24);           //
DigitalOut Bit3(p23);           //

int rawUS_data[5]={0,0,0,0,0};  // raw data{chan1,chan2,chan3,chan4,chan5}

int US1_mean[MAX]={0,0,0,0,0,0,0,0,0,0};        // Structures holding recently recorded distances for each respective
int US2_mean[MAX]={0,0,0,0,0,0,0,0,0,0};        // ultrasonic sensor.
int US3_mean[MAX]={0,0,0,0,0,0,0,0,0,0};
int US4_mean[MAX]={0,0,0,0,0,0,0,0,0,0};
int US5_mean[MAX]={0,0,0,0,0,0,0,0,0,0};

std::string telemFile;          // file holding info about telem data gathered since last power on

void setActiveUS(int chan);

void setActiveUS(int chan){
    switch(chan){
        case 0:
            //ultrasonic 1
            break;
        
        case 1:
            //ultrasonic 2
            break;
            
        case 2:
            //ultrasonic 3
            break;
        
        case 3:
            //ultrasonic 4
            break;
        
        case 4:
            //ultrasonic 5
            break;
        
    }
}

int getPing(){
    int result=0;   
    //write ultrasonic code
    //return measured value
    
    return result;      //should just do "return [operation that gives result];" for efficiency
}

// Opens set file for logging, appends some data, then returns.
// This method is only called for speed and heading changes.
void telemUpdate(float steer, float speed) {
    std::ofstream stream;
    stream.open(telemFile, std::ios::app);
    stream << "steer:" << data << " speed:" << speed << "\n";
}

// Opens set file for logging, appends some data, then returns.
// This method is only called for ambient temperature readings.
void telemUpdate(float temp) {
    std::ofstream stream;
    stream.open(telemFile, std::ios::app);
    stream << "temp:" << temp << "\n";
}

// Alters the cars steering angle based on the float passed to it.
// Float s is valued between -1 and +1. ERRONEOUS VALUES ARE IGNORED.
void turnWheels(float s) {
    if (s++>=0&&s<=2) {steer.pulsewidth(s/2000+0.001}}                          // This is Nicolae's code, simplified and included by Frank
    telemUpdate()
}

// Alters the cars velocity based on the float passed to it.
// Float s is valued between -1 and +1. ERRONEOUS VALUES ARE IGNORED.
void setSpeed(float s) {
    if (s++>=0&&s<=2) {speed.pulsewidth(s/2000+0.001)}                          // THIS CODE IS NOT VERY GOOD and will not work without changes
    telemUpdate()                                                               // Someone who knows how PWM works - fix this please
}


int main() {
    int iCount = 0;
    int measured = 0;
    
    while(iCount <= 5){
        setActiveUS(iCount);
        measured = getPing();
        rawUS_data[iCount] = measured;
    }

    US1_mean[0]=rawUS_data[0];
    US2_mean[0]=rawUS_data[1];
    US3_mean[0]=rawUS_data[2];
    US4_mean[0]=rawUS_data[3];
    US5_mean[0]=rawUS_data[4];

}