Basic Atom Pro code for Ping)))

Hello everyone,

I am new to this form and the basic atom. I have used the the basic stamp and the ping sonar with good results but whatever I do I can’t get the ping to work with the basic atom pro. :frowning: Some example code or a subroutine to read the sonar would be helpful.

Thanks,
Space Junk

Sry, I guess I should have posted in the other ping topic but it seemed to be getting crowded, and I’m new. Thanks.

I wish I could help you bud but I’m not that far into the programming learning curve either.
:slight_smile:

My next code project is to write a routine to read the PING sensor. I have one on the turret of my rover W.A.L.T.E.R. :smiley: Hopefully I can post some code late next week or the following week.

8-Dale

That would be a great help. Just one question, what is W.A.L.T.E.R.? Is there a topic I missed with some pics?

Spacejunk

Yes, W.A.L.T.E.R. is my octagonal shaped two wheel rover. There is an Octabot topic and a W.A.L.T.E.R. topic in the Rovers forum. The Octabot topic has the earliest stages of W.A.L.T.E.R. and the other topic has the current stages of development.

I have a PING sensor on W.A.L.T.E.R.'s turret but it is not yet active. I hope to get code for that in and working within a week or two.

8-Dale

Anyone else have some working code? I have a competition coming up and still have lots to do. :slight_smile:

I have been working with the PING sensor and my Basic ATOM on W.A.L.T.E.R. I am starting to get reasonable readings back from it. :slight_smile: I have been analyzing the Basic STAMP PING code from the Parallax documentation and I think I am finally understanding how the PING works.

I still need to do some serious distance checking to be sure my readings are as close as they can be to the actual distance. It is looking quite good as I do rough distance checks. :slight_smile:

Here is the code I have so far:

[code]’
’ PING Ultrasonic Sonar Ranger

PING con p15 ’ PING Ultrasonic Ranger I/O pin
PING_Trigger con 5 ; Trigger pulse length in uS
PING_Threshld con 25 ’ PING detection threshold


’ PING Ultrasonic Sonar Ranger

PING_Detected var bit ’ 1 = Detected, 0 = Nothing detected
PING_Distance var word ’ PING object detection distance
PING_Echo var word ’ PING echo duration
PING_Scale var long ’ Divisor for distance units conversion

’ Check the Ultrasonic Sonar Ranger on the Pan/Tilt Turret
Read_PING
’ if makenoise then
’ sound speaker,[dur\note13, dur\note14, dur\note15, dur\note16]
’ endif

’ Start the PING detection cycle
low PING

' Send the PING sensor pulse
PULSOUT PING, PING_Trigger

' Read the pulse echo return time
PULSIN PING, 1, PING_Echo

' Create the divisor for cm distance conversion
PING_Scale.highword = 0
PING_Scale.lowword = 2251			' 0.03434 * 65535 cm conversion factor

result = PING_Echo */ PING_Scale
PING_Distance = result / 2

return[/code]

8-Dale

To change that to the atom pro would just mean changing PING_Trigger to 10. Am I right? And what is “makenoise”?

The makenoise variable is just a switch I use to turn sound on and off so the robot can go into and out of “stealth” mode. :slight_smile:

I am not sure changing PING_Trigger to 10 is the only required change to convert my code to the PRO.

I am pretty happy with the way this code is working so far, since I had to figure out how the BS2 code worked before I could make mine work properly. I will be fine tuning the code a bit, but there should not be any major changes now.

8-Dale

Hi SpaceJunk, have you tried this code to see if it works? You could simply comment out the noise functions. Depending on which IDE Linuxguy is using you would need to update the durations as the new IDE for the atom is in Microseconds and the Pro IDE is is milliseconds.

The code looks good to me, but I don’t have the Ping sensor to try it with.
On the Pro you could alternatively do something like define PING_RESULT as a floating point and try something like:

PING_DISTANCE var float
...
PING_Distance = TOFLOAT(PING_Echo) * .03434 / 2

Again I have not tried it, but the Atom Pro IDE manual suggests going to floating point when you need functions like */

LinuxGuy - :smiley: Maybe I will have to get some of these sensors and try it out :smiley: !!!

Kurt

I am using the latest v5.3.0.1 IDE for the Basic ATOM and I have the v8.0.0 IDE for the PRO. Isn’t it the other way around with the PRO having better resolution and being in uS?

The code is giving what looks to be reasonable results for the rough checks I have done so far.

My code should at least give a good starting point. Even though I have not fine tuned it yet, I think the returned results are usable as is.

I guess I will have to wait until I can get an Atom PRO starter kit to do more work on my code since I have hit the limit of what I can stuff into a Basic Atom. I have so many things I want to do, that I have to decide which thing I want to do first with limited funds.

Evolution is sending me 4 HS475 servos, so if I get two more I can fill out my BRAT. If I can get two more servos and an Atom PRO starter kit, that would give me two microcontroller brains. I could switch the PRO between W.A.L.T.E.R. and the BRAT (still unamed) when I want to work on each one.

Then I can convert all my current code to the Atom PRO and have versions for both microcontrollers. :smiley: Increased code base for the Atom PRO will be a good thing. :slight_smile:

8-Dale

Yes I did try it out but I have no idea what’s wrong. I am new to the basic atom so it is tough for me. I have used a basic stamp before and I am good at coding with it. Floating point math!?!?!? :confused: I can hardly do regular atom math!

Wait! I have a blinky sensor!! But I can’t get the terminal window working. Could you add some code to transmit to the atom pro terminal.

Thanks

All it seems to do is transmit “hello world” over and over again.

This is what I can come up with:

The light on the sonar is flashing. My power supply is stable but this leads me to think the atom pro is reseting. The sonar is connected properly and has power. I have tried 3 diffrent modules but none work. The battery is charged. My ocilliscope tells me the supply isn’t dropping.

Thanks

While I don’t have this device I did find that program did want to reset over and over again…

So I started a quick debug process. If you simply add additional Serout command after each main statement in the ping function you find that it appears to rest the processor during the pulsin command. Trying the other form of the command syntax caused the code to stop resetting. I am not sure if it is returning anything valid… But you might try changing the function to something like:

[code]PING var word ’ PING Ultrasonic Ranger I/O pin
Read_PING[PING]
’ Start the PING detection cycle
low PING

’ Send the PING sensor pulse
PULSOUT PING, PING_Trigger

’ Read the pulse echo return time
PULSIN PING, 1, RP_TimeOut, 1, PING_Echo

’ Create the divisor for cm distance conversion
PING_Scale.highword = 0
PING_Scale.lowword = 2251 ’ 0.03434 * 65535 cm conversion factor

PING_Distance = (PING_Echo */ PING_Scale ) / 2

return PING_DISTANCE

RP_TimeOut
serout s_out, i9600, “Pulse Timeout”, 13, 10]
return 0xffff
[/code]

I looked at the basic micro forum and it looks like there may be problems with the pulsin command. My guess is that you could work around this with the RCTime command. I don’t have the time right now to make this work properly, but you might try something like:

...
low Ping

PULSOUT PING, PING_Trigger

RCTIME PING, 1, PING_Echo      ' Wait for the signal to go high

if PING_ECH > SomeTimetout then 
    goto Error
endif
RCTIME PING, 0, PING_Echo      ' Wait for it to go low again - this is our time

...
' Create the divisor...

Good Luck

Thank you very much. It works perfectly. It returns accurate numbers right up to 1cm! :smiley:

One thing. How does the subroutine take a value on the atom pro? :confused: I have used this feture on the SX chip in SX/B. Is it like SX/B where you can use any var as the output?

Here is the code I used:

' 
' PING Ultrasonic Sonar Ranger 
' 
PING_Trigger con 5         ; Trigger pulse length in uS 
PING_Threshld con 25      ' PING detection threshold 

MyDistance var word 
LastDistance var word 

LastDistance = 0xffff 

   serout s_out, i9600, "Hello World", 13, 10] 

main 
   gosub Read_PING[15], MyDistance 
    
   if MyDistance <> LastDistance then 
      serout s_out, i9600, "Distance: ", sdec MyDistance, 13, 10] 
      LastDistance = MyDistance 
   endif 

   pause 250 
   goto main 

' 
' PING Ultrasonic Sonar Ranger 
' 
PING_Distance var word      ' PING object detection distance 
PING_Echo var word         ' PING echo duration 
PING_Scale var long         ' Divisor for distance units conversion 

' Check the Ultrasonic Sonar Ranger on the Pan/Tilt Turret 
PING var byte            ' PING Ultrasonic Ranger I/O pin 
Read_PING[PING] 
   ' Start the PING detection cycle 
   low Ping 

PULSOUT PING, PING_Trigger 

RCTIME PING, 1, PING_Echo      ' Wait for the signal to go high 

if PING_ECHO > 65000 then 
    goto Error 
endif 
RCTIME PING, 0, PING_Echo      ' Wait for it to go low again - this is our time

   ' Create the divisor for cm distance conversion 
   PING_Scale.highword = 0 
   PING_Scale.lowword = 2251         ' 0.03434 * 65535 cm conversion factor 

   PING_Distance = (PING_Echo */ PING_Scale ) / 2 

   return PING_DISTANCE 

ERROR
goto Main

Thanks again, I don’t know how long I’ve been working on this. This is the first code that works. :smiley:

You are welcome. I am glad it is working now!!!

The new Atom PRO IDE does allow parameters to be passed to functions, which I used in this function. It is very primitive in that it does not do any type checking or the like. Also the Parameters are still global variables, so the call (gosub) simply moves the values into the variables… I believe I read somewhere on this forum that new IDE for the Atom may also support this.

One quick look at the code, I see that at your Error lable you do a goto main. This will leave the return address on the stack, which could cause problems. You should instead either slightly restructure the code such that you return a value like 0xffff or use the exception command.

Kurt