Segmentation fault
Lana Eliason <[email protected]>
| Newsgroups | gmane.science.robotics.playerstage |
|---|---|
| Message-ID | <[email protected]> |
Hi guys, sorry it's me again with another problem. I'm getting a
segmentation fault on the lines '*turnSpeed = avoidTurnSpeed;' in the code
below (sorry I don't know how to format the text to look like code on my
email client), I was wondering if anyone could give me an insight as to why?
It used to work fine before, just all of a sudden it's started falling over
there. It is actually a function in some other code that acts on the same
variable (turnSpeed), which could be breaking it elsewhere but I don't know.
I will put the whole code out of the way at the bottom if it helps - saves
you scrolling through the whole thing :)
I wish I could give you more information but I do not have a debugger so the
above is literally all I know :(
//--------------------------------------------
> // AVOID
> //--------------------------------------------
>
> //function for detecting obstacles and avoiding if necessary
> void AvoidObstacles(double *forwardSpeed, double *turnSpeed, double
> *sideSpeed, \
> SonarProxy &sp)
> {
> //will avoid obstacles closer than 3.5m;
> double avoidDistance = 3.5;
> //will turn away at 50 degrees/sec
> int avoidTurnSpeed = 50;
> int criticalAvoidDistance = 0.75;
>
> //left corner is sonar no. 1
> //right corner is sonar no. 2
>
> //sp[0] right
> //sp[1] left
> //sp[2] centre
> //if there is something in front of the uav
> printf("0: %f, 1: %f, 2: %f \n", sp[0], sp[1], sp[2]);
>
>
>
> if(sp[2] < criticalAvoidDistance || sp[1] < criticalAvoidDistance ||
> sp[0] < criticalAvoidDistance)
> {
> printf("reversing\n");
> *forwardSpeed = -1;
> return;
>
> }
> printf("starting avoidance \n");
> if(sp[2] < avoidDistance || sp[1] < avoidDistance || sp[0] <
> avoidDistance)
> {
> printf("Entered if. \n");
> //if something is in front of the UAV, and nothing on left
> if(sp[2] < avoidDistance && sp[1] >= avoidDistance)
> {
>
> //turn left
> *turnSpeed = avoidTurnSpeed;
> printf("turnSpeed: %i \n", *turnSpeed);
> printf("avoiding obstacle\n");
> return;
> }
> //else if something is in front and nothing on right
> else if(sp[2] < avoidDistance && sp[0] >= avoidDistance)
> {
>
> //turn right
> *turnSpeed = (-1)*avoidTurnSpeed;
> printf("turnSpeed: %i \n", *turnSpeed);
> printf("avoiding obstacle\n");
> return;
> }
> //if something on left and not in front
> else if(sp[1] < avoidDistance)
> {
>
> //turn right
> *turnSpeed = (-1)*avoidTurnSpeed;
> printf("turnSpeed: %i \n", *turnSpeed);
> printf("avoiding obstacle\n");
> return;
> }
> //else if something on right and not in front
> else if(sp[2] < avoidDistance)
> {
>
> //turn left
> *turnSpeed = avoidTurnSpeed;
> printf("turnSpeed: %i \n", *turnSpeed);
> printf("avoiding obstacle\n");
> return;
> }
>
> else if(sp[2] < avoidDistance && sp[1] < avoidDistance && sp[0] <
> avoidDistance)
> {
> printf("backing off\n");
> //back off a little bit
> *forwardSpeed = -1;
> *turnSpeed = 2*avoidTurnSpeed;
> printf("avoiding obstacle\n");
> return;
> }
> //if just the frontal sonar registering an obstacle
> else
> {
> *forwardSpeed = 0;
> *turnSpeed = 2*avoidTurnSpeed;
> printf("avoiding obstacle\n");
> return;
> }
>
> }
>
> return; //do nothing
> }
>
>
Thanks very much,
Lana
_________________________________________________________________________________
Full code if needed:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <libplayerc++/playerc++.h>
using namespace PlayerCc;
double forwardSpeed, turnSpeed;
//--------------------------------------------
// WANDER
//--------------------------------------------
//function for making the UAV fly in a random direction
void Wander(double *forwardSpeed, double *sideSpeed, double *turnSpeed)
{
printf("wandering\n");
double fspeed, sspeed, tspeed;
fspeed = 1.5;
// sspeed = 0;
tspeed = 0;
*forwardSpeed = fspeed;
*turnSpeed = tspeed;
//*sideSpeed = sspeed;
}
//--------------------------------------------
// AVOID
//--------------------------------------------
//function for detecting obstacles and avoiding if necessary
void AvoidObstacles(double *forwardSpeed, double *turnSpeed, double
*sideSpeed, \
SonarProxy &sp)
{
//will avoid obstacles closer than 3.5m;
double avoidDistance = 3.5;
//will turn away at 50 degrees/sec
int avoidTurnSpeed = 50;
int criticalAvoidDistance = 0.75;
//left corner is sonar no. 1
//right corner is sonar no. 2
//sp[0] right
//sp[1] left
//sp[2] centre
//if there is something in front of the uav
printf("0: %f, 1: %f, 2: %f \n", sp[0], sp[1], sp[2]);
if(sp[2] < criticalAvoidDistance || sp[1] < criticalAvoidDistance ||
sp[0] < criticalAvoidDistance)
{
printf("reversing\n");
*forwardSpeed = -1;
return;
}
printf("starting avoidance \n");
if(sp[2] < avoidDistance || sp[1] < avoidDistance || sp[0] <
avoidDistance)
{
printf("Entered if. \n");
//if something is in front of the UAV, and nothing on left
if(sp[2] < avoidDistance && sp[1] >= avoidDistance)
{
//turn left
*turnSpeed = avoidTurnSpeed;
printf("turnSpeed: %i \n", *turnSpeed);
printf("avoiding obstacle\n");
return;
}
//else if something is in front and nothing on right
else if(sp[2] < avoidDistance && sp[0] >= avoidDistance)
{
//turn right
*turnSpeed = (-1)*avoidTurnSpeed;
printf("turnSpeed: %i \n", *turnSpeed);
printf("avoiding obstacle\n");
return;
}
//if something on left and not in front
else if(sp[1] < avoidDistance)
{
//turn right
*turnSpeed = (-1)*avoidTurnSpeed;
printf("turnSpeed: %i \n", *turnSpeed);
printf("avoiding obstacle\n");
return;
}
//else if something on right and not in front
else if(sp[2] < avoidDistance)
{
//turn left
*turnSpeed = avoidTurnSpeed;
printf("turnSpeed: %i \n", *turnSpeed);
printf("avoiding obstacle\n");
return;
}
else if(sp[2] < avoidDistance && sp[1] < avoidDistance && sp[0] <
avoidDistance)
{
printf("backing off\n");
//back off a little bit
*forwardSpeed = -1;
*turnSpeed = 2*avoidTurnSpeed;
printf("avoiding obstacle\n");
return;
}
//if just the frontal sonar registering an obstacle
else
{
*forwardSpeed = 0;
*turnSpeed = 2*avoidTurnSpeed;
printf("avoiding obstacle\n");
return;
}
}
return; //do nothing
}
//----------------------------------------
// TARGET
//----------------------------------------
void TargetUAV(double *forwardSpeed, double*sideSpeed, double *turnSpeed, \
BlobfinderProxy &bfp, SonarProxy &sp)
{
int i, centre;
playerc_blobfinder_blob_t blob;
//turns at 50 degrees/sec
int turningSpeed = 50;
//will avoid obstacles closer than 3.5m;
double avoidDistance = 3.5;
//set initial green marker found variable to false
bool greenFound = false;
int noBlobs = bfp.GetCount();
//if there is something directly in front of the UAV (within collision
distance), stop
if(sp[0] < avoidDistance)
{
printf("sp[1] = %f, avoidDistance: %f \n", sp[1], avoidDistance);
*forwardSpeed = 0;
*turnSpeed = 0;
return;
}
//if no UAV in sight, set wander speed and return
if (noBlobs == 0)
{
*forwardSpeed = 1.5;
return;
}
printf("In target \n");
/*number of pixels away from the image centre a blob
can be to be in front of the robot*/
int margin = 20;
int greenBlobArea = 0;
int greenBlob = 0;
bool followCatchUp = false;
int catchUpSpeed = 2.5;
//int sSpeed = 0;
printf("noBlobs: %i\n", noBlobs);
//find the largest blob
for(i=0; i<noBlobs; i++)
{
//get blob from proxy
playerc_blobfinder_blob_t currBlob = bfp[i];
//if colour of blob is green..
printf("In loop, blob.color = %i \n", currBlob.color);
if( currBlob.color == 0x00FF00 ) // green
{
printf("green found \n");
greenFound = true;
greenBlob = i;
greenBlobArea = currBlob.area;
if (currBlob.range > avoidDistance)
followCatchUp = true;
else
*forwardSpeed = 1.5;
}
}
blob = bfp[greenBlob];
// find centre of image
centre = bfp.GetWidth()/2;
printf("NoBlobs: %i \n", noBlobs);
//adjust turn to centre the blob in image
/*if the blob's centre is within some margin of the image
centre then move forwards, otherwise turn so that it is
centred. */
if (greenFound == true && noBlobs > 1)
{
printf("following\n");
//blob to the left of centre
//if looking at side of UAV
if(noBlobs == 2)
{
//if green on right of UAV (i.e. facing left side of UAV)
if(greenBlob == 1)
{
printf("turning left, 2, greenBlob = %i \n", greenBlob);
//turn left
*turnSpeed = turningSpeed;
}
//else if green on left of UAV (i.e. facing right side of UAV)
else if( greenBlob == 0)
{
//turn right
*turnSpeed = -turningSpeed;
printf("finishing turning \n");
}
}
if(blob.x < centre - margin)
{
printf("section 1\n");
*forwardSpeed = 1.5;
//turn left
*turnSpeed = turningSpeed;
// *sideSpeed = -sSpeed;
if (followCatchUp == true)
{
*forwardSpeed = catchUpSpeed;
followCatchUp = false;
}
}
//blob to the right of centre
else if(blob.x > centre + margin)
{
*forwardSpeed = 1.5;
//turn right
*turnSpeed = -turningSpeed;
//*sideSpeed = sSpeed;
if (followCatchUp == true)
{
*forwardSpeed = catchUpSpeed;
followCatchUp = false;
}
}
//otherwise go straight ahead
else
{
*forwardSpeed = 1.5;
*turnSpeed = 0;
if (followCatchUp == true)
{
*forwardSpeed = catchUpSpeed;
followCatchUp = false;
}
}
return;
}
}
//----------------------------------------
// MAIN
//----------------------------------------
//Code enters here on execution
int main(int argc, char *argv[])
{
printf("Entered main\n");
fflush(stdout);
using namespace PlayerCc;
PlayerClient Brian("localhost",6665);
Position2dProxy p2dProxy(&Brian,1);
SonarProxy sonarProxy(&Brian,1);
BlobfinderProxy blobProxy(&Brian,1);
//shared Simulation proxy...
SimulationProxy simProxy(&Brian,0);
double forwardSpeed, sideSpeed, turnSpeed;
srand(time(NULL));
p2dProxy.RequestGeom();
sonarProxy.RequestGeom();
Brian.Read();
while(true)
{
//read from proxies
Brian.Read();
fflush(stdout);
//wander
// Wander(&forwardSpeed, &sideSpeed, &turnSpeed);
//else
TargetUAV(&forwardSpeed, 0, &turnSpeed, blobProxy, sonarProxy);
printf("finished target function\n");
if(blobProxy.GetCount()==0)
{
//avoid obstacles
AvoidObstacles(&forwardSpeed, 0, &turnSpeed, sonarProxy);
printf("finished avoiding\n");
}
//set motors
p2dProxy.SetSpeed(forwardSpeed, 0, dtor(turnSpeed));
// sleep(1);
}
}
------------------------------------------------------------------------------
What You Don't Know About Data Connectivity CAN Hurt You
This paper provides an overview of data connectivity, details
its effect on application quality, and explores various alternative
solutions. http://p.sf.net/sfu/progress-d2d
_______________________________________________
Playerstage-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-developers