Shop OBEX P1 Docs P2 Docs Learn Events
Combining codes of Ping))) Sensor and Servo connected to Propeller Board inside S2 — Parallax Forums

Combining codes of Ping))) Sensor and Servo connected to Propeller Board inside S2

ASDevarajuASDevaraju Posts: 8
edited 2013-03-15 18:16 in Propeller 1
Hi, I am currently working an S2 robot with a Propeller Board inside(I remember it being a Propeller BOE). The external hardware added to the robot are the PING))) Ultrasonic Sensor and the Servo(Parallax Standard Servo). I am currently stuck on programming both the sensor and the servo to work at the same time. Here are the codes for the Ping))), The servo, and for the final code, respectively: PING_TEST:
CON
   _xinfreq = 5_000_000
   _clkmode = xtal1 + pll16x

   PING_PIN      = 5
   PCBAUD        = 115_200
 obj
   d     :       "Parallax Serial Terminal"
   ping  :       "Ping"
 PUB Go
   d.start(PCBAUD)
   repeat
      d.home
      d.str(string("Ping distance: "))
      d.dec(ping.Millimeters(PING_PIN))
      d.str(string(" mm   "))
      PauseMs(20)
 pub PauseMs(duration)
  waitcnt(cnt + (clkfreq/1000)*duration)

S2_SERVO_TEST:
con
   _xinfreq = 5_000_000
   _clkmode = xtal1 + pll16x
   SERVO_PIN = 4
 pub RunServo
   'set pin to be an output
   dira[SERVO_PIN]~~
   repeat
      PulseOut(1000, SERVO_PIN)

      PulseOut(1500, SERVO_PIN)

      PulseOut(2000, SERVO_PIN)

 pub PulseOut(time, pin)
   repeat 100
      outa[pin]~~
      waitcnt(cnt + (clkfreq/1_000_000)*time)
      outa[pin]~
      PauseMs(20)
 pub PauseMs(duration)
    waitcnt(cnt + (clkfreq/1000)*duration)

And the PING_SERVO:
CON
   _xinfreq = 5_000_000
   _clkmode = xtal1 + pll16x

   PING_PIN      = 5
   PCBAUD        = 115_200
   SERVO_PIN = 4 
 obj
   d     :       "Parallax Serial Terminal"
   ping  :       "Ping"
 PUB Go
   d.start(PCBAUD)
   repeat
      d.home
   d.str(string("Ping distance: "))
   d.dec(ping.Millimeters(PING_PIN))
      d.str(string(" mm   "))
      PauseMs(20)
 pub RunServo
   'set pin to be an output
   dira[SERVO_PIN]~~
   repeat
      PulseOut(1000, SERVO_PIN)

      PulseOut(1500, SERVO_PIN)

      PulseOut(2000, SERVO_PIN)

 pub PulseOut(time, pin)
   repeat 100
      outa[pin]~~
      waitcnt(cnt + (clkfreq/1_000_000)*time)
      outa[pin]~
      PauseMs(20)
 pub PauseMs(duration)
    waitcnt(cnt + (clkfreq/1000)*duration)
My classmate told that the main function to work on is the PulseOut function in order to get both the Ping)) sensor and the Servo to work, but I am still unsure.

Comments

  • kwinnkwinn Posts: 8,697
    edited 2013-03-11 21:43
    Can you post your code between {code} and {/code} statements but instead of the upper case (shifted) curly braces use the lower case (non shifted) square braces. That makes the code much more readable.

    In essence what you will need to do is have a main routine that continuously executes the ping code, the pulse out code, and whatever other code you need in a repeat loop.
  • ASDevarajuASDevaraju Posts: 8
    edited 2013-03-12 15:01
    kwinn wrote: »
    Can you post your code between {code} and {/code} statements but instead of the upper case (shifted) curly braces use the lower case (non shifted) square braces. That makes the code much more readable.

    In essence what you will need to do is have a main routine that continuously executes the ping code, the pulse out code, and whatever other code you need in a repeat loop.
    Ok I will do so. So, basically like in C++/Java coding, I would need to create a main routine where it will call both the ping code and the pulse out code at the same time, right?
  • lardomlardom Posts: 1,659
    edited 2013-03-13 09:48
    Take a look at my code. My servo pin is (26) and my Ping pin is (27) so you have to change the pin assigments. The 'waitcnt' gives the servo the time it needs to move. After the servo move is complete the Ping object is called. When the Ping result is recieved the next servo command is executed and so on.
    Also, when you post code, use the # icon.
  • ASDevarajuASDevaraju Posts: 8
    edited 2013-03-15 16:34
    lardom wrote: »
    Take a look at my code. My servo pin is (26) and my Ping pin is (27) so you have to change the pin assigments. The 'waitcnt' gives the servo the time it needs to move. After the servo move is complete the Ping object is called. When the Ping result is recieved the next servo command is executed and so on.
    Also, when you post code, use the # icon.
    I compiled the code and the Propeller tool said it can't find the object in the editor tabs, work folder, or library. Is there another way?
  • ASDevarajuASDevaraju Posts: 8
    edited 2013-03-15 16:38
    Hi everyone,
    I was thinking of coding methods in the program. Here is what it would be:
    PUB callMethod(method,var1,var2....)
      switch method
        METHOD1: Go(var1,var2...)
        METHOD2: PulseOut(var1,var2...)
    
     PUB Go(variables)
      its code under here.
    
     PUB PulseOut(variables)
      its code is under here.
    
    I haven't implement the code yet. But, I know that if I were to methods, then I will need to put callMethod in a loop so it does things more than once. Any suggestions to this?
  • lardomlardom Posts: 1,659
    edited 2013-03-15 16:52
    Hi ASDevaraju, Create a new folder on your desktop. It will make it easy to find your files. Open the zip and click File, Select All then Copy Files to Clipboard. Finally Paste them into the folder you created. All of the files have to be in the same folder.
    Post again and let me know how you did.

    I forgot to mention to look at the README file first to see the top object.
  • ASDevarajuASDevaraju Posts: 8
    edited 2013-03-15 17:12
    Hi lardom, I did as you told me and when I compiled the program, I didn't any results: The SERVO was not rotating and the didn't get any readings. I looked through the code and you forgot to add in the Parallax Serial Terminal. Also, the Propeller Tool didn't completely compile the code into the RAM. Here is what it looks like:
    PST_results.jpg
    . Sorry I didn't expect that the forum would make the image smaller.
    622 x 89 - 16K
  • lardomlardom Posts: 1,659
    edited 2013-03-15 17:21
    Don't worry. I will create a new zip with the serial terminal included. I didn't include it because you already have it. I understand your confusion. I've had to ask for help many times. I'll post it shortly.
  • ASDevarajuASDevaraju Posts: 8
    edited 2013-03-15 17:23
    Oh thanks man. I actually found the ParallaxSerialTerminal.spin file. I just needed to call it.
  • ASDevarajuASDevaraju Posts: 8
    edited 2013-03-15 17:34
    Hey man, do you think it's necessary to add the following code into your code:
    CON
      _xinfreq = 5_000_000
      _clkmode = xtal1 + pll16x
    
      PING_PIN      = 5
      PCBAUD        = 115_200
    
  • lardomlardom Posts: 1,659
    edited 2013-03-15 17:45
    Great! Yes, I added a CON block because the PST will not work properly without it. You'll see it in the new zip. It will allow you to see the values from your Ping)))
  • ASDevarajuASDevaraju Posts: 8
    edited 2013-03-15 18:16
    Hi lardom, Thanks for helping. I got the code working and I got my results. :smile::smile:
    PST_results.jpg
    661 x 414 - 49K
Sign In or Register to comment.