Author Topic: calling multiple sensor functions causing jerkiness  (Read 2861 times)

0 Members and 1 Guest are viewing this topic.

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
calling multiple sensor functions causing jerkiness
« on: August 24, 2008, 12:18:09 PM »
My robot is jerking around the floor

I figured out it is because I am calling the sharpIR servo scanning function followed by my parallax ping sonar function.

How can I sort this out so that calling these two functions constantly in a while loop won't cause my robot to jerk around?

heres the code
Code: [Select]
while(1)
{

  ping();

          //Object Avoider
       
          if (PingVal > 8) {
       scan();
       //if (sharp_IR_reading > scan_thresh) {
        //object on left
         //   if(scan_angle > 456)     // 57 * 8
       //      robot_turn_right();
                    //object on right
           // else if(scan_angle < 328)    // 41 * 8
         //    robot_turn_left();
              // } // end if
               //object is centered
       //else
        robot_go_straight();
           }  // end if
   else {
        while (PingVal < 16) {
         robot_go_back();
     ping();
                } // end while
            }  // end else


even with lots of stuff commented out it does it, thats how i narrowed the problem down to the actual function calls.

would it be better to attach the function calls to interrupts?




Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: calling multiple sensor functions causing jerkiness
« Reply #1 on: August 24, 2008, 04:07:03 PM »
This is called robot oscillation. Basically, when your robot oscillates, it is stuck between PingVal of 15 and 16. When its 15, it goes in reverse, and when its 16 after going in reverse, it immediately goes forward causing PingVal to drop to 15 again. Basically its an infinite loop.

Your code:
Code: [Select]
robot_go_straight();

else
while (PingVal < 16)
robot_go_back();
ping();

Here is one of many solutions:
Code: [Select]
robot_go_straight();

else
while (PingVal < 16)
robot_go_back();
delay_ms(500);//give it time to go in reverse
robot_rotate_left();//if it doesn't turn, it just goes forward into object again
ping();

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: calling multiple sensor functions causing jerkiness
« Reply #2 on: August 24, 2008, 04:54:55 PM »
it does the going backwards part very smooth and just fine, because in that while loop its not calling ping and scan, only ping.

Its only with going forward that causes it to jerk.

I tried attaching it to a timer, but you can't use functions that receive or return values.

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
    • izua electronics
Re: calling multiple sensor functions causing jerkiness
« Reply #3 on: August 24, 2008, 06:05:47 PM »
You haven't thought out the software flow properly.
Basically, you need to read all sensors, process them all, together, and then decide how to move. If you move based on the first one, then move based on the second one, then again the first one, oscillation, as you experience, is extremely likely to occur.

So, read the parallax sonar, then the sharp IR, then decide what to do based on both values. You could do a complete sweep (if any sensor is mounted on a servo) and use the mapped result to decide how to move.

Even if you think your program based on interrupts, it's not really going to help, since the flow will be the same - you analyze sensors separately.
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: calling multiple sensor functions causing jerkiness
« Reply #4 on: August 24, 2008, 06:11:05 PM »
Quote
Its only with going forward that causes it to jerk.
So if you comment out the reverse function, it doesn't jerk?

Or perhaps your sensor is very noisy (look at sensor values with serial)? Or that the sonar is sometimes detecting the ground (try aiming the sonar up higher)?

Quote
I tried attaching it to a timer, but you can't use functions that receive or return values.
What do you mean?

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: calling multiple sensor functions causing jerkiness
« Reply #5 on: August 24, 2008, 06:31:00 PM »
@ izua

I had already tried combining the two at the very beginning of the while loop, example:
Code: [Select]
while(1) {
     ping();
     scan();
...

Same problem, and besides the $50 robot w/ sharp IR source code doesn't do a full servo sweep before moving and it works just fine w/o jerking around.

@ Admin
-The functionality of throwing it in reverse works fine, no jerkiness there.
Its only with the first part that causes it (specifically calling the two functions)
Code: [Select]
while(1)
{
         ping();
         //scan();
         if (PingVal > 8) {
            scan();
           robot_go_straight();
          }  // end if
          ...


-using only ping or scan in the code by themselves and it works fine.


-The timerAttach documentation in the header file timerx8.h states that.
00235 //      myOverflowFunction must be defined with no return value and no arguments:

« Last Edit: August 24, 2008, 06:36:51 PM by pomprocker »

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: calling multiple sensor functions causing jerkiness
« Reply #6 on: August 25, 2008, 12:36:10 PM »
hey i just realized, isnt fuzzy the omniwheeled robot doing scanning and sonar at the same time?

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: calling multiple sensor functions causing jerkiness
« Reply #7 on: September 01, 2008, 05:44:24 PM »
Quote
using only ping or scan in the code by themselves and it works fine.
So both of these work fine?

Code: [Select]
while(1)
{
         ping();
         //scan();
Code: [Select]
while(1)
{
         //ping();
         scan();

But not this?

Code: [Select]
while(1)
{
         ping();
         scan();

Quote
hey i just realized, isnt fuzzy the omniwheeled robot doing scanning and sonar at the same time?
Yeap, 3 sonar and 3 sharp IR ;D
Although it was written for the PIC16F877, it shouldn't be hard to take code from it.

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: calling multiple sensor functions causing jerkiness
« Reply #8 on: September 01, 2008, 09:43:59 PM »
I THINK I figured this out, but then I wasnt able to program the board anymore....this episode is to be continued...