Shop OBEX P1 Docs P2 Docs Learn Events
FlexBASIC P2 PING — Parallax Forums

FlexBASIC P2 PING

Does anybody have any P2 code for using the PING?

I looked in the Parallax github site for P2 objects, and nothing there. I did find some code for the P1, but the PASM part is way above my capabilities. I was thinking about trying to fit that code into a FlexBASIC Sub, but then I would not be able to share it with FlexC, if I ever needed to use FlexC.

Thanks
Ray

Comments

  • pik33pik33 Posts: 2,366
    edited 2022-01-29 08:16

    I have (cheap, chinese) ultrasonic distance detectors in my robot, but they have separate pins for echo and trigger and they are connected via inverters to a P2 and there are 8 of these in the robot.

    It is doable in pure Basic as you have to make a pulse - the code makes 40 us pulse, I don't know what length and polarization is needed for PING:

      pinhi(us_trig)                                       
      waitus 40                                               
      pinlo(us_trig)   
    

    Then you have to start a smart pin

     pinstart(us_echo),  P_COUNT_HIGHS,0,0)
    

    Wait several miiiseconds letting the pin count the pulse length and after 10 miliiseconds or something like this, read the pin

    r=rdpin(us_echo)/something        
    

    This something is 1676 in my code to have the distance in milimeters at 285 MHz CPU speed. You have to compute this constant using sound speed, your CPU speed and length units.

    us_trig and us_echo are the same in PING so you will need to stop the smartpin after the reading and before make the next pulse.

  • @Rsadeika said:
    I looked in the Parallax github site for P2 objects, and nothing there. I did find some code for the P1, but the PASM part is way above my capabilities. I was thinking about trying to fit that code into a FlexBASIC Sub, but then I would not be able to share it with FlexC, if I ever needed to use FlexC.

    ?? You can call FlexBASIC from FlexC, just the same as you can call Spin2 from FlexC. All of the Flex languages can work together.

  • I was watching the latest P2 early adopters youtube video, and I noticed that there is a P2 object for the PING. It is is located in the quick bytes area. The P2 objects are starting to get scattered, hopefully everything will start showing up in one place, again.

    Ray

  • JonnyMacJonnyMac Posts: 9,104
    edited 2022-01-30 21:02

    Here's how I read the Ping))) sensor with the P2 in my object. It's Spin2, so I will only post the method here for you to translate.

    pub ticks(pin) : result | timeout
    
    '' Request distance from sensor on pin
    '' -- returns 1-way distance in microseconds
    '' -- will return 0 if bad or missing sensor
    
      pinclear(pin)                                                 ' reset pin
    
      pinhigh(pin)                                                  ' generate trigger pulse
      waitus(5)
      pinlow(pin)
      waitus(5)                                                     ' let trigger register
    
      timeout := getms()                                            ' start time-out timing
      pinstart(pin, P_HIGH_TICKS, 0, 0)                             ' measure return pulse (system ticks)
      repeat
        if ((getms()-timeout) > 25)                                 ' if > 25ms, abort
          return 0
      until pinread(pin)                                            ' wait for end of pulse
    
      return (rdpin(pin) / (clkfreq / 1_000_000)) >> 1              ' convert pulse to microseconds
    

    This should be easy to translate to FlexBASIC.

Sign In or Register to comment.