Author Topic: question about programming a scanning sharp IR sensor  (Read 2647 times)

0 Members and 1 Guest are viewing this topic.

Offline raptorwesTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
question about programming a scanning sharp IR sensor
« on: April 17, 2011, 09:08:07 PM »
I am trying to modify my photovore ( with axon II) with a front mounted scanning sharp ir sensor for obstacle avoidance and for the last 2 days have not been able to figure out how to program this. I have been reading the servo and sensor files in webbotlib, i have tried to modify Admin's $50 robot/with Sharp IR code and i am getting no where. I am using the project designer, i set up the usb port to out put the readings, i have gone in and checked and the sensor appears to be outputting data. the base code from the project designer makes the servos rotate back and fourth wich is fine for the scanning servo but not the wheel servos.  Does anyone have any code that i can steal for a robot using the axonII, differential drive and a scanning sharp IR (GP2Y0A21YK)

i will post the code that i have but it isn't much
Quote
#include "hardware.h"

// Initialise the hardware
void appInitHardware(void) {
   initHardware();
// Set UART1 to 115200 baud
    uartInit(UART1, 115200);
    // Tell rprintf to output to UART1
    rprintfInit(&uart1SendByte);

}


// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
   return 0;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {



   // -------- Start Switch/Button-------
   // Switch/Button - see switch.h
   
   // To test if it is pressed then
   if(SWITCH_pressed(&button)){
      // pressed
   }
   
   // To test if it is released then
   if(SWITCH_released(&button)){
      // released
   }
   // -------- End   Switch/Button-------

   // -------- Start Marquee-------
   // Marquee - see 'segled.h'
   // Before using the Marquee you need to redirect rprintf to write to it
   // This can be done using
   Writer old = rprintfInit(marqueeGetWriter(&marquee));
   
   // All rprintf output will then be sent to the marquee but will not
   // display until an end-of-line eg "\n" has been sent. Example:-
   // rprintf("Hello World\n");
   
   // If the endDelay is non-zero then the marquee will scroll
   // forever or until you call: marqueeStop(&marquee);
   
   // If the endDelay is zero then the marquee will stop once
   // the entire line has been shown ('one-shot' mode)
   
   // In 'one-shot' mode then you may want to make sure that
   // a previous line has finished before you display a second line.
   // This can be done as follows:-
   marqueeSetEndDelay(&marquee,0); // Make sure we are in one-shot mode
   if(marqueeIsActive(&marquee)==FALSE){
        if(loopCount==1){
           rprintf("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
        }else{
         rprintf("Loop=%u\n",(unsigned)loopCount); // Put the loop count
        }
   }
   
   // Restore rprintf back to its previous location
   rprintfInit(old);
   // -------- End   Marquee-------

   // -------- Start Sharp GP2-------
   // Read the Sharp GP2 and store the result in ADC_ir_sensor.distance.cm
   distanceRead(ADC_ir_sensor);
   
   // The value can be printed using %u eg rprintf("Distance=%u",ADC_ir_sensor.distance.cm);
   // or dumped using:
   rprintf("ADC_ir_sensor: ");
   distanceDump(ADC_ir_sensor);
   rprintfCRLF();
   // -------- End   Sharp GP2-------

   // -------- Start Actuators -------
   // To control your.motors/servos then see actuators.h in the manual   // To retrieve the required speed of Left_wheel use:
   // DRIVE_SPEED speed=act_getSpeed(Left_wheel);
   // To set the required speed of Left_wheel use:
   // act_setSpeed(Left_wheel,speed);
   // This example will move the motors back and forth using the loopStart time:
   TICK_COUNT ms = loopStart / 1000;      // Get current time in ms
   int16_t now = ms % (TICK_COUNT)10000;    // 10 sec for a full swing
   if(now >= (int16_t)5000){            // Goes from 0ms...5000ms
      now = (int16_t)10000 - now;         // then 5000ms...0ms
   }
   // Map it into DRIVE_SPEED range
   DRIVE_SPEED speed = interpolate(now, 0, 5000, DRIVE_SPEED_MIN, DRIVE_SPEED_MAX);
   // Set speed for all motors/servos
   act_setSpeed(&Left_wheel,speed);
   act_setSpeed(&Right_wheel,speed);
   act_setSpeed(&IR_servo,speed);
   // -------- End   Actuators -------

   return 0;
}

Offline raptorwesTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
Re: question about programming a scanning sharp IR sensor
« Reply #1 on: April 20, 2011, 08:16:37 PM »
Ok i get it, nobody wants to do this for me and i under stand. I just wanted to compare "good " code with what i believe to be the not so good  code i am using. I have continued to try to get this to work with but with no luck.

this is the code i have been trying to compile maybe someone can look at this and let me know where i am going wrong  ..i hope.. ;D


Quote
#include "hardware.h"
//global variables
int sharp_IR_reading=0;

int scan_thresh=0;//threshold value of scanning sensor

int scan_angle=30;//position of scanner, in units of servo command
int max_scan_angle=56;//maximum position scanner can rotate to (57)

// Initialise the hardware
void appInitHardware(void) {
   initHardware();
// Set UART1 to 115200 baud
    uartInit(UART1, 115200);
    // Tell rprintf to output to UART1
    rprintfInit(&uart1SendByte);

}


// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
   return 0;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {



   // -------- Start Switch/Button-------
   // Switch/Button - see switch.h
   
   // To test if it is pressed then
   if(SWITCH_pressed(&button)){
      // pressed
   }
   
   // To test if it is released then
   if(SWITCH_released(&button)){
      // released
   }
   // -------- End   Switch/Button-------

   // -------- Start Marquee-------
   // Marquee - see 'segled.h'
   // Before using the Marquee you need to redirect rprintf to write to it
   // This can be done using
   Writer old = rprintfInit(marqueeGetWriter(&marquee));
   
   // All rprintf output will then be sent to the marquee but will not
   // display until an end-of-line eg "\n" has been sent. Example:-
   // rprintf("Hello World\n");
   
   // If the endDelay is non-zero then the marquee will scroll
   // forever or until you call: marqueeStop(&marquee);
   
   // If the endDelay is zero then the marquee will stop once
   // the entire line has been shown ('one-shot' mode)
   
   // In 'one-shot' mode then you may want to make sure that
   // a previous line has finished before you display a second line.
   // This can be done as follows:-
   marqueeSetEndDelay(&marquee,0); // Make sure we are in one-shot mode
   if(marqueeIsActive(&marquee)==FALSE){
        if(loopCount==1){
           rprintf("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
        }else{
         rprintf("Loop=%u\n",(unsigned)loopCount); // Put the loop count
        }
   }
   
   // Restore rprintf back to its previous location
   rprintfInit(old);
   // -------- End   Marquee-------

   // -------- Start Sharp GP2-------
   // Read the Sharp GP2 and store the result in ADC_ir_sensor.distance.cm
   distanceRead(ADC_ir_sensor);
   
   // The value can be printed using %u eg rprintf("Distance=%u",ADC_ir_sensor.distance.cm);
   // or dumped using:
   rprintf("ADC_ir_sensor: ");
   distanceDump(ADC_ir_sensor);
   rprintfCRLF();
   // -------- End   Sharp GP2-------

   // -------- Start Actuators -------
   // To control your.motors/servos then see actuators.h in the manual   // To retrieve the required speed of Left_wheel use:
   // DRIVE_SPEED speed=act_getSpeed(Left_wheel);
   // To set the required speed of Left_wheel use:
   // act_setSpeed(Left_wheel,speed);
   // This example will move the motors back and forth using the loopStart time:
//this function causes scanning servo to center on edge of object
void scan(void)
   {
   //lower (-) goes right
   //higher (+) goes left
   //30 far right, 50 straight, 56 far left (until it jams)

   

   sharp_IR_reading=a2dConvert8bit(3);
   
   if (sharp_IR_reading > scan_thresh)//object detected
      {
      if (scan_angle>41) //overflow protection
         scan_angle-=2;//scan right
      }
   else //object not detected
      {
      if (scan_angle<=max_scan_angle) //maximum servo angle
         scan_angle+=2; //scan left
      else //if scanned all the way, this forces it to start over
         scan_angle=30;
      }
      
   //servo scan code
   servo_scan(scan_angle);
   }


//automatically calculates threshold value before run
void autocalibrate(void)
   {
   scan_thresh=a2dConvert8bit(3);//sensor reading
   }

int main(void)
   {
   //LED_on();

   initialize();
   
   delay_cycles(42000);//two second wait delay


//find thresholds
   autocalibrate();

   //LED_off();


   while(1)
      {
      scan();
      
      //object on left
      if(scan_angle > 57)
         robot_turn_left();

      //object on right
      else if(scan_angle < 41)
         robot_turn_right();

      //object is centered
      else
         robot_go_straight();

      delay_cycles(400);//a small delay to prevent crazy oscillations
      }
   /*********ADD YOUR CODE ABOVE THIS LINE **********/

   return 0;
   }


I get 1 error and 7 warnings they are :
Quote
Build started 20.4.2011 at 22:07:21
avr-gcc -g -Wall -DF_CPU=16000000 -mmcu=atmega640 -gdwarf-2 -std=gnu99 -fpack-struct -fshort-enums  -funsigned-char -funsigned-bitfields -I"../../WebbotLib"  -MD -MP -MT irtest.o -MF irtest.d -O0 -c -o irtest.o irtest.c
irtest.c: In function 'scan':
irtest.c:118: warning: implicit declaration of function 'servo_scan'
irtest.c: In function 'appControl':
irtest.c:128: warning: 'main' is normally a non-static function
irtest.c: In function 'main':
irtest.c:132: warning: implicit declaration of function 'initialize'
irtest.c:149: warning: implicit declaration of function 'robot_turn_left'
irtest.c:153: warning: implicit declaration of function 'robot_turn_right'
irtest.c:157: warning: implicit declaration of function 'robot_go_straight'
irtest.c: In function 'appControl':
irtest.c:164: error: expected declaration or statement at end of input
irtest.c:164: warning: control reaches end of non-void function
make: *** [irtest.o] Error 1
Build failed with 1 errors and 7 warnings...

thanks in advance for any help

« Last Edit: April 20, 2011, 08:21:09 PM by raptorwes »

Offline rbtying

  • Supreme Robot
  • *****
  • Posts: 452
  • Helpful? 31
Re: question about programming a scanning sharp IR sensor
« Reply #2 on: April 20, 2011, 10:04:16 PM »
You're calling more than a few functions that don't exist in your code:

Code: [Select]
'initialize'
'robot_turn_left'
'robot_turn_right'
'robot_go_straight
'servo_scan'

Since you're using Webbotlib, you don't use a main() loop, instead, you use the appControl loop (clearly marked in the comment 'this is the main loop')

You should probably take a look at the Webbotlib examples - they clearly explain the programming structure, and that's what's keeping your code from compiling. http://webbot.org.uk/WebbotLibDocs/27746.html <-- example!

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: question about programming a scanning sharp IR sensor
« Reply #3 on: April 21, 2011, 07:02:20 AM »
Ok i get it, nobody wants to do this for me and i under stand. I just wanted to compare "good " code with what i believe to be the not so good  code i am using. I have continued to try to get this to work with but with no luck.

this is the code i have been trying to compile maybe someone can look at this and let me know where i am going wrong  ..i hope.. ;D

I get 1 error and 7 warnings they are :
Quote
Build started 20.4.2011 at 22:07:21
avr-gcc -g -Wall -DF_CPU=16000000 -mmcu=atmega640 -gdwarf-2 -std=gnu99 -fpack-struct -fshort-enums  -funsigned-char -funsigned-bitfields -I"../../WebbotLib"  -MD -MP -MT irtest.o -MF irtest.d -O0 -c -o irtest.o irtest.c
irtest.c: In function 'scan':
irtest.c:118: warning: implicit declaration of function 'servo_scan'
irtest.c: In function 'appControl':
irtest.c:128: warning: 'main' is normally a non-static function
irtest.c: In function 'main':
irtest.c:132: warning: implicit declaration of function 'initialize'
irtest.c:149: warning: implicit declaration of function 'robot_turn_left'
irtest.c:153: warning: implicit declaration of function 'robot_turn_right'
irtest.c:157: warning: implicit declaration of function 'robot_go_straight'
irtest.c: In function 'appControl':
irtest.c:164: error: expected declaration or statement at end of input
irtest.c:164: warning: control reaches end of non-void function
make: *** [irtest.o] Error 1
Build failed with 1 errors and 7 warnings...

thanks in advance for any help


I know when starting out error messages can be bewildering, but learning to read them is a valuable skill.

The warnings mean that you're missing prototypes for functions, which mean that you're probably missing one or more header files.

The error is harder to figure out, but if you have an editor that does indentation for you it would be easy to see that you're missing a closing curly brace at the end of the appControl() function. The second warning gives a clue in this regard as well. It says that main should be a non-static function, and as you can see, main() is subsumed inside the appControl() function because of the missing brace.

Joe