Julesnaps / Mbed 2 deprecated Linefollowproject

Dependencies:   m3pi mbed

main.cpp

Committer:
mikkelbredholt
Date:
2022-10-06
Revision:
11:d13596393d56
Parent:
10:9b04c532b57b
Parent:
9:7b9094864268
Child:
12:6fe33785b0f4

File content as of revision 11:d13596393d56:

#include "mbed.h"
#include "m3pi.h"

m3pi m3pi;

// Minimum and maximum motor speeds
#define MAX 1.0
#define MIN 0

// PID terms
#define P_TERM 1
#define I_TERM 0
#define D_TERM 20



// Prototypes
int PitTest(int gotoPit); 

int main() {
    
    m3pi.sensor_auto_calibrate();
    
    /*Base program Variable initiation*/
    float right;
    float left;
    float current_pos_of_line = 0.0;
    float previous_pos_of_line = 0.0;
    float derivative,proportional,integral = 0;
    float power;
    float speed = MAX;

    /*Team 7 Variabels*/
    
    int gotoPit = 0; // wether or not the robot is heading to pit. Initialstate false. 
    int ccount; //used to count cycles
    

    
    /*Printing secret cat mission*/
    m3pi.cls();
    
    m3pi.locate(0,0);
    m3pi.printf("eliminate");
    m3pi.locate(0,1);
    m3pi.printf("all cats");
    wait(200.0);
    
    m3pi.cls();
    m3pi.locate(0,0);
    m3pi.printf("%f.3 ",m3pi.battery());
    m3pi.locate(0,1);
    m3pi.printf("%f.3 ",m3pi.pot_voltage());
    wait(200.0);
    m3pi.cls();


    
    while (1) {
        
        if (ccount %100 == 0 && gotoPit=0){
            gotoPit=PitTest(gotoPit);
            }
    
        // Get the position of the line.
        current_pos_of_line = m3pi.line_position();        
        proportional = current_pos_of_line;
        
        // Compute the derivative
        derivative = current_pos_of_line - previous_pos_of_line;
        
        // Compute the integral
        integral += proportional;
        
        // Remember the last position.
        previous_pos_of_line = current_pos_of_line;
        
        // Compute the power
        power = (proportional * (P_TERM) ) + (integral*(I_TERM)) + (derivative*(D_TERM)) ;
        
        // Compute new speeds   
        right = speed+power;
        left  = speed-power;
        
        // limit checks
        if (right < MIN)
            right = MIN;
        else if (right > MAX)
            right = MAX;
            
        if (left < MIN)
            left = MIN;
        else if (left > MAX)
            left = MAX;
            
       // set speed 
        m3pi.left_motor(left);
        m3pi.right_motor(right);
        
    cyclecount++;
    }
}


int PitTest(int gotoPit){
/* Test the batteri voltage if the robot is not headed for pit */ 
    
    float batVol = m3pi.battery();  //Storing battery voltage in variable
    const float BATVOLTRESHOLD = 3.0; // Treshold i volt
    
    /*Digital outs*/
    DigitalOut led1(LED1);
    led1 = 0; // Turn off led 1 on the embed

    /*Updates battery voltage*/
    batVol = m3pi.battery(); 
    
    /*Test if the voltage is below the treshold if so turn on go to pit mode*/
    if batteryVoltage <= BATVOLTRESHOLD (){  
        gotoPit = 1; // set goto pit condition
        led1 = 1; // trun on Led 1
        m3pi.cls();
        m3pi.locate(0,0);
        m3pi.printf("Going to");
        m3pi.locate(0,1);
        m3pi.printf("PIT Party");
    }
}


void LED_Control(int ledNumber, int state){
    if ledNumber == 1{
        DigitalOut led1(state);
    }
    if ledNumber == 2{
        DigitalOut led1(state);
    }
    if ledNumber == 3{
        DigitalOut led1(state);
    }
    if ledNumber == 4{
        DigitalOut led1(state);
    }
}