What is wrong with this?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
therien
Posts: 9
Joined: 20 Dec 2010, 18:18

What is wrong with this?

Post by therien »

hey there,

me and a friend programmed this nxt 2.0 robot (with nxc) but found some problems.
The idea is to make a robot (from the standard pack) that will search for objects and once if finds one, go there, pick it up, and bring it back to its original spot, then drop it there.
I will attach the program below but first I''ll explain some things.
Motor B and C are the wheels (B is left, C is right).
Sensor 1 is the ultrasonic sensor
Sensor 2 is a touch sensor installed in the grabber to detect if it has already picked up the object or not.
The robot has 3 wheels, 2 front wheels (B and C) and one rearwheel which can turn 360 degrees losely.
This is to make it able to turn on the spot.
The problem is that it does not pick up the objects on the moment it is supposed to, but it starts grabbing later on.
Please have a look at our program and try to find anything that's wrong with it.

Code: Select all

#define one_degree_turn 50
#define distance_step 100
#define grabber_step 100
#define speed 20
int turn_way;
// turn_way == 1 = right && turn_way == 0 = left
int dist_start;
int turn_start;
int temp_turn;
int pre_dist_start;
int grabber;
int dist;
int turn;
int a;
int b;
int c;
task calc_dist_start()
{
     a = dist_start;
     b = dist;
     pre_dist_start=dist_start;
     dist_start = floor(sqrt(pow(a,2.0)+pow(b,2.0)-2*a*b*cosd(180-turn-turn_start)));
}

task calc_turn_start()
{
     a = dist_start;
     b = dist;
     c = pre_dist_start;
     temp_turn = acosd((pow(a,2.0)+pow(b,2.0)-pow(c,2.0))/(2*a*b));
     if (turn>180)
     {
     turn_start = turn_start-temp_turn;
     }
     else
     {
     turn_start = turn_start+temp_turn;
     }
     turn = 0;
     dist = 0;
}

task drop_load()
{
     while (grabber != 0)
     {
           OnRev(OUT_A,25);
           Wait(grabber_step);
           grabber--;
     }
}

task main()
{
dist_start = 2;
SetSensorLowspeed(IN_1);
SetSensor(IN_2,SENSOR_TOUCH);
SetSensorMode(IN_2,SENSOR_MODE_RAW);
SetSensor(IN_3,SENSOR_TOUCH);
SetSensorMode(IN_3,SENSOR_MODE_RAW);
while (true)
{
       if (SensorUS(IN_1)<20)  // found!
       {
           while (SensorUS(IN_1)>6) // go there
           {
                dist++;
                OnFwd(OUT_BC,speed);
                Wait(distance_step);
           }
       OnFwd(OUT_BC,0);
       while (grabber != 6 && SENSOR_2 <10) // here it is supposed to start picking up
       {
           OnFwd(OUT_A,25);
           Wait(grabber_step);
           grabber++;
       }
       start calc_dist_start;
       Wait(100);
       start calc_turn_start;
       Wait(100);
           while (turn_start>360)
           {
             turn_start = turn_start-360;
           }
           if (turn_start>180) // right turn
           {
             turn_start = turn_start-180;
             while (turn_start != 0)
             {
                   OnFwd(OUT_B,speed);
                   OnRev(OUT_C,speed);
                   Wait(one_degree_turn);
                   turn_start--;
             }
           }
           else if (turn_start<0) // right turn
           {
             turn_start = turn_start*-1;
             while (turn_start != 0)
             {
                   OnFwd(OUT_B,speed);
                   OnRev(OUT_C,speed);
                   Wait(one_degree_turn);
                   turn_start--;
             }
           }
           else     // left turn
           {
              while (turn_start != 0)
              {
                   OnFwd(OUT_C,speed);
                   OnRev(OUT_B,speed);
                   Wait(one_degree_turn);
                   turn_start--;
              }
           }
           while (dist_start != 2)
           {
              OnFwd(OUT_BC,speed);
              dist_start--;
              Wait(distance_step);
           }
           start drop_load;
           Wait(1000);
       }
       else
       {
           while (SensorUS(IN_1)>100 || SensorUS(IN_1)<6)
           {
                   OnFwd(OUT_C,speed);
                   OnRev(OUT_B,speed);
                   Wait(one_degree_turn);
                   turn++;
                   if(turn = 360)
                   {
                           turn = 0;
                           Wait(90*one_degree_turn);
                           turn = turn + 90;
                           OnFwd(OUT_BC,speed);
                           Wait(50*distance_step);
                           start calc_dist_start;
                           start calc_turn_start;
                   }
           }
           Off(OUT_BC);
       }
}

// end main
}


afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: What is wrong with this?

Post by afanofosc »

I would try replacing your tasks (other than main) with simple subroutines. At least 2 of them look like they really are just subroutines.

So replace "task" with "void" and where you have start taskname; replace it with subroutinename(); E.G., calc_dist_start(); or calc_turn_start(); I can't tell for sure about the drop load task or what exactly it isn't doing that it should be doing.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
therien
Posts: 9
Joined: 20 Dec 2010, 18:18

Re: What is wrong with this?

Post by therien »

Well it seems to be doing stuff in a different order than it's supposed to.
Maybe the tasks that are supposed to be subroutines cause this, we will probably try this tomorrow, thanks a lot for the tip.
If it won't make any difference, I'll try to explain it better afterwards.
therien
Posts: 9
Joined: 20 Dec 2010, 18:18

Re: What is wrong with this?

Post by therien »

Now things start to get really weird...
I just copied the code into my own bricxcc (instead of the one on my friend's pc) and I got a truckload of errors.
When I replace the tasks you mentioned with subroutines (void) I still get the errors.
This line sends out the errors:

Code: Select all

dist_start = floor(sqrt(pow(a,2.0)+pow(b,2.0)-2*a*b*cosd(180-turn-turn_start)));
The first error is Undefined identifier floor.
Afterwards comes the same for pow, cosd and acosd (on a different line), ';' expected, too many arguments and a lot of unmatched close parenthesis.
Any idea how this could have happened or how to solve it?
All help is greatly appreciated, since we need to finish this robot this month (including a big report explaining everything).
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: What is wrong with this?

Post by muntoo »

Image

Make sure to install the latest one, and the latest test release.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
therien
Posts: 9
Joined: 20 Dec 2010, 18:18

Re: What is wrong with this?

Post by therien »

I had the preferences set to the sticky topic I read on it.
I now turned off the automatic firmware (like the screenshot has) and I had no more errors.
My friend didn't have any errors today either (he did yesterday) and he has it turned on (he didn't change anything over night).
We'll try the robot again soon and I'll reply again, thanks for the help so far.
thijssie93
Posts: 1
Joined: 11 Jan 2011, 19:16

Re: What is wrong with this?

Post by thijssie93 »

These two problems are solved at the moment, the initial problem as well. The errors have magically disappeared over the night. Yesterday the same program did give errors, today with the saved version from yesterday I got no errors... We will probably need some help soon with other problems :roll:. I'm 'the friend' btw.
Ty for so far
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: What is wrong with this?

Post by mightor »

thijssie93 wrote:The errors have magically disappeared over the night. Yesterday the same program did give errors, today with the saved version from yesterday I got no errors...
Our crack team of bug fixers found out where you lived and broke into your house to fix the bugs.

We aim to please.

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
therien
Posts: 9
Joined: 20 Dec 2010, 18:18

Re: What is wrong with this?

Post by therien »

Would you mind doing that again but this time to balance out the robot? (It's not my home anyway)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: What is wrong with this?

Post by muntoo »

therien wrote:Would you mind doing that again but this time to balance out the robot? (It's not my home anyway)
Don't worry, this time we'll cook it [your friend's house] to perfection! :twisted:
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 9 guests