Julesnaps / Mbed 2 deprecated Linefollowproject

Dependencies:   m3pi mbed

main.cpp

Committer:
vehus
Date:
2022-10-11
Revision:
15:8b76add42254
Parent:
14:12fb3e326911
Child:
17:3fcc43140382

File content as of revision 15:8b76add42254:

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

m3pi m3pi;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

// 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(void);   // Test if to robot needs to goto pit 
void InitialMessages(void); // Prints initial message to the LCD 
void LED_Control(int ledNumber, int state); //turn ledNumber to 1=on, 0 = off
void PitStop(void); // 
void Blink(int ledNumber); // make ledNumber blinik

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 = 0; //used to count cycles
    

    
    /*Printing secret cat mission*/
    InitialMessages();
 

    
    while (1) {
        /* If cycle count divided by 100 does not have a rest. test if pit */
        if (ccount % 100 == 0 && gotoPit == 0)
        {
            gotoPit = PitTest();
        }
        if (gotoPit == 1)
        {
            PitStop ();
            
        }
            
    
        // 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);
        
    ccount++;
    }
    
    
}

void InitialMessages(void){
    /*Prints iniatl secret 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();
}
int PitTest(void){
/* Test the batteri voltage if the robot is not headed for pit */ 
    
    const float BATVOLTTRESHOLD = 3.0; // Treshold i volt
    int result = 0;
    
    /*Test if the voltage is below the threshold if so turn on go to pit mode*/
    if (m3pi.battery() <= BATVOLTTRESHOLD ){  
        result = 1; // Set goto pit condition
        LED_Control(1, 1);
        m3pi.cls();
        m3pi.locate(0,0);
        m3pi.printf("Going to");
        m3pi.locate(0,1);
        m3pi.printf("PIT Party");
    }
    return result;
}

void LED_Control(int ledNumber, int state){
    //LED1 on if robot is looking for pit
    if (ledNumber == 1)
    {
        led1 = state;
    }
    if (ledNumber == 2)
    {
        led2 = state;
    }
    if (ledNumber == 3)
    {
        led3 = state;
    }
    if (ledNumber == 4)
    {
        led4 = state;
    }
}

void Blink(int ledNumber)
{
    int a = 2;
    LED_Control (ledNumber , 0);
    wait(a);
    LED_Control (ledNumber , 1);
    wait(a);
    }

void PitStop(void)
{
    /* Testing alternative stop function
    m3pi.left_motor(0);
    m3pi.right_motor(0);
    */
    m3pi.stop();       // stop all engine
    
    // increase counter with one
    while (1)
    {
        Blink (1);      // signal in pit
        
/* missing input to stop blink. */
        
    }
}