Asked — Edited

Compass Heading Tracking

I have been messing with this for a while and can't make progress. I want to track forward on a set compass heading. If there is a variation from the heading I want to make a correction turn back toward the heading. I have made the compass to read with a +/- 5 degree tolerance. For setup I am using a deviation of 30 degrees, but will narrow it to 10 degrees once it is working.

$compass_setting = 150


$compass_setting2 = $compass_setting + 30


$compass_setting3 = $compass_setting - 30

:loop

  if ($CompassHeading = $compass_setting)

    Forward()

  endif
  if ($CompassHeading = $compass_setting3)


 Left() 

endif 
  
  
  Goto(:loop)

I have tried a number of variations but no luck.

Any ideas?

Ron


ARC Pro

Upgrade to ARC Pro

Subscribe to ARC Pro, and your robot will become a canvas for your imagination, limited only by your creativity.

#1  

First, you say compass_setting as variables, but compass_heading as another.

Second, you have capitals where you shouldn't.

Third, you dont need individual ifs for each variable. Use elseif to lump it together.

#2  

Seems like a bit of midnight coding if you ask me!XD

A bit of an oversight I often do myself!

#4  

You should also put a sleep() command in your loop to avoid flooding the channel....

PRO
Synthiam
#5  

And a sleep will give the robot at least some time to respond to the movement command.

Continually sending a Left() to the robot will keep reset the movement to left. it's already going left, so no point to keep sending left().

Probably a good idea to check what the current direction is before sending the command again.

#6  

Thanks, I appreciate the guidance. I will try again with the suggestions in mind. Ron

#7  

Using the above example, and answers, I got the full script working by adding the required Sleep commands Dj and Richard suggested. I am now working on getting all the test scripts in one project and running together.

Thanks for the help.

Ron

User-inserted image

PRO
Synthiam
#8  

Cool! That's coming along well:D

PRO
Synthiam
#9  

Oh, i should add that since you are using continuous rotation servos for moving... that you can also adjust the speed in which the robot moves.

So you could do something like this...

User-inserted image


SayEZBWait("Which direction shall i face?")

$Speech_Response = (WaitForSpeech(5, "East", "West"))

if ($Speech_Response = "East")

  SayEZBWait(("I am moving" + $Speech_Response))

  $DirectionToFace = 90


elseif ($Speech_Response = "West")

  SayEZBWait(("I am moving" + $Speech_Response))

  $DirectionToFace = 180


else

  SayEZBWait("I didn't hear you")

  # Stop running this script
  Halt()


endif
ControlCommand("MPU9150", Init)

repeat($count, 0, 10, 1)

  ControlCommand("MPU9150", RunOnce)

  if ($CompassHeading < $DirectionToFace)

    SetSpeed(50)
    Right()


  else

    SetSpeed(50)
    Left()


  endif
  sleep(1000)


endrepeat
ClearVariable("$count")

Here's the Blockly workspace: facecompassdirection.ezblockly

#10  

Hi Dj,

I will load it and give it a try. My friend will have another skill added to the list.

I run a bunch of separate codes. One is the Radar avoidance, facial tracking moving the head, tracking moving the eyes, (the camera is mounted under her nose) and compass tracking. The voice is a Cepstral male voice (for now). His name is Antonn. He enjoys having conversations using responses to questions and when he gets lazy he makes requests to Alexa. He also runs light and outlet controls which I built (before the many smart devices became available). It is nice to ask him to make coffee in the morning and letting me know when it is done, or operating lights.

Thanks for the code,

Ron