Hi all,
I have a robot which detects an object using hsl filtering and a blob counter to draw a rectangle around the object. What I am trying to do is get the robot to move to its right if the object is on the right side, else it moves to the left. Essentially I want it to work as a goalkeeper, so as the object comes towards it, it moves into the objects path.
I am far from a C# expert and have tried various ways to get it to function properly, to no avail. When the program runs the robot seems to randomly move left and right as it pleases, quite often doing so until it loses sight of the object, in which case it just carries on going.
Below is my controller method, any chance somebody could help me fix this so it runs as intended? If required I can include my DetectObject method or the form used to call the controller method, as I am not sure where the problem lies.
public void Bangbang()
{
int speed = 5;
while (true)
{
Bitmap image = API.Camera.GetImage();
Rectangle rects = DetectObject(image);
//RecImage(image);
double error = (rects.X + rects.Width / 2) - (image.Width / 2);
if (error > 0)
//API.Movement.ManualDrive.RotateRight(speed);
API.Movement.ManualDrive.StraightRight(speed);
else //if (error < 0)
//API.Movement.ManualDrive.RotateLeft(speed);
API.Movement.ManualDrive.StraightLeft(speed);
//else
// API.Movement.ManualDrive.Stop();
}
}
I had a slightly different piece of code recommended to me in which if (error > 0)
was replaced with if (error)
, however this produces an error about converting double to bool. I also tried Convert.ToBoolean
but this didn't seem to do anything.
I've been stuck on this program for days and could really use a hand! Thank you :)