Quantcast
Channel: Learn with BlocklyProp — Parallax Forums
Viewing all articles
Browse latest Browse all 293

code question for self driving truck

$
0
0
hello all,

I am having a hard time figuring out why i cant get a piece of code to work. any ideas would be welcomed.

quick sum up. I took apart my kids rc ford f150. two dc motors control the toy. using prop Activity Board, ping sensor, and two ir leds with detectors; i am trying to make a self driving car.

problem: everything seems to work except the motor control for steering. i wrote functions for everything and am running all sensors in their own cog.

i cute most of the code out to make it easier to read...but the section i am having trouble with is here.

the motor just spins continually. it should turn a little to left or right depending on the ir sensor...then self correct to start position once sensors are cleared of walls.

thanks in advance for looking at it
#include "simpletools.h"                      // Include simpletools header
#include "ping.h"                             // Include ping header
#include "abdrive.h"


int pdar(void);
int irLeft(void);
int irRight(void);
void printer(void);

//void mcDrive(void);
void mcSteer(void);
//void backup(void);


unsigned int stack1[40 + 25]; 


int main()                                         // Main function
{

   
 cogstart(pdar, NULL, stack1, sizeof(stack1));      // Launch ping function into another cog 
 cog_run(mcSteer, 128);                               // launch steering into cog
 //cog_run(mcDrive, 128);
 cog_run(irLeft, 128);
 cog_run(irRight, 128);
  
 printer();                                        // running this function on main cog
 }

int pdar(void)
{
     while(1)                                      // Repeat indefinitely
  {
    int cmDist = ping_cm(15);                     // Get cm distance from Ping)))
              
    pause(200);                                   // Wait 1/5 second
    return cmDist;                                 
  }
}

int irLeft(void)
{
  while(1)
  {
    int irLeft;                            //  variable
    int irPin   = 9;
    int detPin  = 8;
    
    freqout(irPin, 1, 38000);                  // Left IR LED light
    irLeft = input(detPin);                     // Check left IR detector

    return irLeft;
  }
}

int irRight(void)
{
  while(1)
  {
    int irRight;                          // variable
    int irPin   = 12;
    int detPin  = 11;
    
    freqout(irPin, 1, 38000);                // Right IR LED light
    irRight = input(detPin);                  // Check right IR detector

    return irRight;
  }
}
    
    
  
void printer(void)
{
   while(1)
    {
      int p = pdar(); 
      int irR = irRight();
      int irL = irLeft();                           
     
    print("%c C.M. from Object = %d\n\n pirLeft = %d, irRight = %d",       
           HOME, p,      irL,       irR);
                                   
                    
    } 
 }
 
 
   
void mcSteer(void)                                      // stearing wheel command set
{

  set_outputs(5, 2, 0b0000);
  
  set_directions(5, 2, 0b1111);
  
	pwm_start(1000); 
                    

  int p           = pdar();
  int irL         = irLeft();
  int irR         = irRight();
  int right       = 4;
  int left        = 5;
  int turnSpeed   = 100;
  int turnTime    = 200;
  
  //(pin, pwm channel, 800/1000)
  
  while(1)
  {
      
     if(irL == 0 && irR == 0 && p > 0 && p <= 10)
       {
          do
          {
            pause(500);                                       // Stops any motor control for 1/2 second while
          }while(irL == 0 && irR == 0 && p > 0 && p <= 25);   //         in use by mcDrive function.                         
       }                
        
        
      if(irL == 0 && irR == 1)
       {     
          int i =  0;                
          do
            {
              do
                {
                  i++;
                  pwm_set(right, 1,turnSpeed);                  // turn right   
  		           } while(i < turnTime);
             
            }while(irL==0);              
            
          if(irL == 1)
            {
              int i = 0;
              do
                {
                  i++;
                  pwm_set(left, 1,turnSpeed);                          
  		           } while(i < turnTime); 
            }                                             
       }      
       
       
      else if(irR == 0 && irL == 1)
       {                        
          int i = 0;
       
          do
            {
              do
                {
                  i++;
                  pwm_set(left, 1,turnSpeed);                   // turn left         
  		           } while(i < turnTime);
            }while(irL==0);
       
          if(irL == 1)
            {
              int i = 0;
              do
                {
                  i++;
                  pwm_set(right, 1,turnSpeed);                          
  		           } while(i < turnTime);                                   
            }
       }          
                   
  }
   
}      

Viewing all articles
Browse latest Browse all 293

Trending Articles