basic code

Dependencies:   FatFileSystem MSCFileSystem btbee m3pi_ng mbed

Fork of Robot by IESS

main.cpp

Committer:
nbtavis
Date:
2015-06-03
Revision:
4:0cdad3a8484b
Parent:
3:bae8eb81a9d7

File content as of revision 4:0cdad3a8484b:

#include "mbed.h"
#include "btbee.h"
#include "m3pi_ng.h"
m3pi robot;
DigitalIn m3pi_IN[]= {(p12),(p21)}; // IR sensor and Knopf


Timer timer;
Timer time_wait;


#define MAX 0.4
#define MIN 0 

#define P_TERM 1
#define I_TERM 0
#define D_TERM 20 



int main(){
   
   robot.sensor_auto_calibrate();
   wait(2.0);
   
   float right;
   float left;
   float current_pos = 0.0;
   float previous_pos =0.0;
   float derivative, proportional, integral = 0; 
   float power;
   float speed = MAX;
   

  int y =1;


   while(y)
   {
   //Get raw sensor values
   int x [5];
    robot.calibrated_sensor(x);
   
  
   
   //Check to make sure battery isn't low 
   if (robot.battery() < 2.4)
    {timer.stop();
        break;}
        
        
    else if(x[0] > 300 && x[4] > 300) 
    {  while( x[0]> 300 && x[4] > 300)
     {robot.calibrated_sensor(x);}
       y = 0;           
             }
    else if (x[0]>300 && x[4] < 20)   
      {} 

    else if (x[0]<20 && x[4] > 300)  
    { while 
    
    
   
   current_pos = robot.line_position();
   
   //compute the integral
   proportional = current_pos; 
   
   //compute the integral  
   derivative = current_pos - previous_pos;
   
   //compute the integral
   integral =+ proportional;
   
   //remember the last position.
   previous_pos = current_pos;
   
   // compute the power 
  power = (proportional*(P_TERM)) + (integral*(I_TERM)) + (derivative*(D_TERM));
  
   //computer 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>MIN)
    left = MAX;
   
   //set speed
   
   robot.left_motor(left);
   robot.right_motor(right);
   
   wait((5-time_wait.read_ms())/1000);
   }
   
   
   
   robot.stop();
  
     char hail[]={'V','1','5','O','4','E','4','C','8','D','8','E','8','C','8','D','8'
,'E','8','F','4','D','8','E','8','F','8','D','8','E','8','F','8','G'
,'4','A','4','E','1','6','E','1','6','F','8','C','8','D','8','E','8'
,'G','8','E','8','D','8','C','4'};
    int numb = 59;
     
  //  robot.playtune(hail,numb);
   
   
   
           
    }