Society of Robots - Robot Forum

Software => Software => Topic started by: Daibo on August 02, 2011, 04:27:25 PM

Title: Meet Tuppie!! He needs help with his brains.
Post by: Daibo on August 02, 2011, 04:27:25 PM
(http://img26.imageshack.us/img26/4350/tuppie001.th.jpg)

Hi, I am new to the robot world and I have built my very first robot "Tuppie". Now after days and days of trying to figure out why the code I am trying isn't working. I decided to break down and ask for help. I am trying to build a object follower. I am using the axon 2 with a sharp ir and 3 hitec HS-311. Two for the drive wheels and one for the scanning servo. I can't seem to get the scanning servo to "scan" and I also believe the sensor isn't working and was hoping someone could take a look at the code and tell me whats wrong with it. I have tryed to copy other programs but no success has come from it.  The code itself comes from both webbotlib and some of admins code for the 50 dollar IR upgrade --servo scan(scan_angle)--- when added in at the proper place causes a compiler error. I've read through webbotlib multiple times and have tried different coding but to no improvement. Thank you in advance

The code as is will compile and uploads  to the Axon 2.
Code: [Select]
#include "sys/axon2.h"
#include "servos.h"
#include "a2d.h"
#include "sensors/distance/sharp/GP2.h"
 
// Define one distance sensors connected to ADC channels 0
#define sensordistance ADC_NUMBER_TO_CHANNEL(0)
 
// Define three servos
SERVO left = MAKE_SERVO(FALSE, B5,1500, 500);
SERVO right = MAKE_SERVO(TRUE, B6,1500, 500);
SERVO scan = MAKE_SERVO(FALSE, B7,1500, 500);
// Define IR sensor

Sharp_GP2D12 sensor = MAKE_Sharp_GP2D12(ADC0);
 
// Create the list - remember to place an & at the
// start of each servo name and to use PROGMEM
SERVO_LIST PROGMEM servos[] = {&left,&right,&scan};
 
// Create a driver for the list of servos
SERVO_DRIVER bank1 = MAKE_SERVO_DRIVER(servos);


 
//the larger this number, the more likely your robot will drive straight
#define threshold 8


 
// In my initialising code - pass the list of servos to control
void appInitHardware(void){
    // Initialise the servo controller
    servoPWMInit(&bank1);
    // Initialise the sensor
    distanceInit(sensor);
    // Give each servo a start value of 'stop'
    act_setSpeed(&left, 0);
    act_setSpeed(&right,0);
    act_setSpeed(&scan, 0);
   
}
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
    return 0;
}
 
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart)

{
   
// This is for sensor data
    distanceRead(sensor);


int sharp_IR_reading=0;

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

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


//this function causes scanning servo to center on edge of object
void scan(void)

{

sharp_IR_reading=a2dConvert8bit(0);


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;
}


}


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


    if(scan_angle > 57){
        // go left
        act_setSpeed(&left,DRIVE_SPEED_MIN);
        act_setSpeed(&right,DRIVE_SPEED_MAX);
    }else if(scan_angle < 41){
        // go right
        act_setSpeed(&left,DRIVE_SPEED_MAX);
        act_setSpeed(&right,DRIVE_SPEED_MIN);
    }else{
        // Go forwards
        act_setSpeed(&left,DRIVE_SPEED_MAX);
        act_setSpeed(&right,DRIVE_SPEED_MAX);
    }
    return 20000; // wait for 20ms to stop crazy oscillations
}
Title: Re: Meet Tuppie!! He needs help with his brains.
Post by: adanvasco on August 02, 2011, 07:24:28 PM
Shouldn't you have a main function?
Title: Re: Meet Tuppie!! He needs help with his brains.
Post by: corrado33 on August 02, 2011, 07:35:38 PM
Yes, yes he should have a main function.   :)

Basically what you've done is created all of the functions that your main function would use, now you just need to call them and actually tell your bot what you want it to do. 
Title: Re: Meet Tuppie!! He needs help with his brains.
Post by: Daibo on August 02, 2011, 08:19:23 PM
I was under the impression
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart)
was the start of the Main.
Title: Re: Meet Tuppie!! He needs help with his brains.
Post by: adanvasco on August 02, 2011, 09:42:40 PM
Every C program has a main() function.
Title: Re: Meet Tuppie!! He needs help with his brains.
Post by: Daibo on August 02, 2011, 10:22:17 PM
Yup after I let my brain rest for a few hrs I noticed the mistake which I corrected. I am however still having an error when i try to add servo_scan(scan_angle); into the program. I might be wrong but servo_scan is calling on the servo i declared in the beginning and telling it to go to scan_angle correct?
Title: Re: Meet Tuppie!! He needs help with his brains.
Post by: adanvasco on August 03, 2011, 07:52:54 AM
Can you attach the rest of the header files you are using? I don't see the servo_scan() function anywhere, so it probably is on the servos.h file. I'm not really sure since I don't have an Axon, that's why I need to take a look at the whole code.
Title: Re: Meet Tuppie!! He needs help with his brains.
Post by: mstacho on August 03, 2011, 11:34:19 AM
The program is using webbotlib, right?  That acts like Arduino -- there is no main() in your code, Arduino (webbotlib) handles all of that for you.  It took me a little while to get used to as well...So probably the problem is in the rest of the code.
Title: Re: Meet Tuppie!! He needs help with his brains.
Post by: Daibo on August 03, 2011, 04:15:54 PM
Figured it out, well sort of. It avoids objects instead of tracking them, but a win is a win. Thanks for the help guys.
Title: Re: Meet Tuppie!! He needs help with his brains.
Post by: corrado33 on August 03, 2011, 06:10:31 PM
Hahaha looks like you need to change a < or > sign somewhere.  :)