Shop OBEX P1 Docs P2 Docs Learn Events
More Spin help needed — Parallax Forums

More Spin help needed

NWCCTVNWCCTV Posts: 3,629
edited 2013-07-22 20:33 in Propeller 1
Can anyone tell me what is wrong with the attached code? I am makeing a call to sense.start at the bottom of the program and that portion works fine but my Main code does not work. If I remove the call to Pings and sense.start my main code works fine. Sorry, I have not made any comments yet as I want the code to work first.
Con
                                                         
                                                         
  _CLKMODE = XTAL1 + PLL16X                              
  _XINFREQ = 5_000_000
  
  IRP           = 2        'ir receiver i/o port pin
  PING_Pin      = 13                                     ' I/O Pin For PING)))
  LED1          = 8                                          ' I/O PIN for LED 1
  LED2          = 9                                          ' I/O PIN for LED 2
  ON            = 1
  OFF           = 0                                          
  Distlimit     = 6                                          ' In inches 
Var
  long code
  long range
  
Obj
  pst : "parallax serial terminal"
  irt : "irtest"
 servo: "PropBOE Servos"               'Servo control object
 sense: "Ping Demo_NO_PST_Servo"
  ping   : "ping"
Pub Main
  pst.start(115200)
  irt.start(IRP, @code)       'after this "code" will contain the # for button pressed or 255 if no button is being pressed
  'servo.start
  
    
  repeat
    pst.char(1)
    pst.dec(code)
    pst.chars(32, 3)
    waitcnt(clkfreq/100 + cnt)
    if code == (16)                                         'done, bail
      servo.Set(14, -250)                                    ' P14 servo full speed counterclockwise
      servo.Set (15,250)
    elseif code == (17)
     servo.Set (0,300)
     servo.Set (1,-280)
    elseif code == (19)
     servo.Set (1, 75)
     servo.Set (0, -250)
    elseif code == (18)
     servo.Set (1, 250)
     servo.Set (0, -75) 
    else
     servo.Set(14,-10)       
     servo.Set(15, 45)
     pings
     
Pub pings
    sense.start

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-07-19 13:49
    NWCCTV,

    Without looking at your complete code it's going to be very difficult to determine what is going on. Please use the File-->Archive-->Project and attach that file so that the entire scope of your code can be visualized.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-19 14:15
    Added, Thanks Beau, I kind of forgot about that.
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-07-19 14:34
    NWCCTV,

    One thing that I see is that you have two OBJ references to "PropBoe Servos". One of which looks like it is initiated properly with a "servo.start" but the other does not, but yet the program is trying to set various servos using the "servo.set" command without using "servo.start". And I'm not positive that they won't collide with each other if you try to initialize both of them together, although it might be ok. You just need to use "servo.start" within the "Ping Demo_NO_PST_Servo"
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-19 14:50
    Beau, I have actually tried it both ways with the same results. Sholud I try using a different servo object for one of them?
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-19 14:55
    OK, That did not work either. It seems as if the program is jumping straight to the sense.start command therefor bypassing the Pub Main routine.
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-07-19 15:07
    NWCCTV,

    In the "IR_Servo_Ping" you are starting the 'irt', but there isn't anything to set the 'code' variable. so assuming code=0 then it falls through.... edit.. the code variable is being set, but even a value of 255 would fall through below if no buttons are pressed.

    What code values are displayed when you 'remark' the 'pings' command at the bottom of "IR_Servo_Ping" without pressing any buttons on the IR?

    if code == (16)
    elseif code == (17)
    elseif code == (19)
    elseif code == (18)

    upon which it calls "pings" and launches "Ping Demo_NO_PST_Servo"
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-19 15:27
    16 is the Remote code for the CH UP button, 17 is CH Down, 19 is CH Left and 18 is CH Right. That cod works fine as long as I comment out the call to sense.start which controls the Ping sensor. I have removed all the code for the Ping that brought up the Serial Terminal so as not to interfere with the code for the IR. Both Objects work fine on their own but like I said, once I make the call to the Ping program it is the only one that runs. Am I making sense?

    EDIT: Hold on and I will test what you asked.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-19 15:31
    The code is 255 when not pressed, under that is a 5 and under that is another 5.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-19 15:36
    I am not trying to call up pings if anything is pressed. What I am trying to do is get the PING sensor to work along side my IR Remote. The IR remote will basically only be used in the event the PING does not sense anything. Basically just an override setup.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-07-19 17:00
    Are you still having trouble with this? Is the code in post #1 the most recent version of your code? If not, would you attach an archive of your most recent code?
  • ratronicratronic Posts: 1,451
    edited 2013-07-19 17:48
    NW looking at the code you posted in the first post the program Ping Demo_NO_PST_Servo.spin (sense object) start method will never return causing your main loop to hang. That is because it is stuck in a repeat loop. I see you are trying to operate servo's in that object also.

    Edit: The IR object runs in the background in another cog constantly updating the variable "code" in the calling cog. Maybe you can operate the servo's, sense the ping all from the IR_Servo_ping.spin program?
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-19 17:56
    @Duane Degn, The code I am using is what is in the archive zip file. @ratronic, Yes, I need the servos to operate in both objects. OOne is using a remote control and the other is using a PING sensor. Basically the Remote is for an override in case of problems. I will remove the repeat and see what happens.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-19 18:06
    I have to have a "repeat" function as the program needs to always be sensing if there is anything in front of the Bot. How can I make these work together?
  • ratronicratronic Posts: 1,451
    edited 2013-07-19 18:10
    I don't know if you saw the edit in post#12 but since the servo and IR objects run in other cogs you should be able to do everything you want in IR_Servo_Ping.spin.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-19 18:28
    I tried it that way with no luck. I will mess with it some more to see what happens. It seems that when I tried to call the IR Object it did not work. Or are you saying to not call the object and just code it all in the PING program?

    EDIT: I tried placing the Ping code below the IR code and I get the same thing. The Ping code runs fine but the IR code does not work, like again it is just bypassing it for some reason. Code attached.
    Con
    
    
    _CLKMODE = XTAL1 + PLL16X 
    _XINFREQ = 5_000_000
    
    IRP = 2 'ir receiver i/o port pin
    PING_Pin = 13 ' I/O Pin For PING)))
    LED1 = 8 ' I/O PIN for LED 1
    LED2 = 9 ' I/O PIN for LED 2
    ON = 1
    OFF = 0 
    Distlimit = 6 ' In inches 
    Var
    long code
    long range
    
    Obj
    pst : "parallax serial terminal"
    irt : "irtest"
    servo: "PropBOE Servos" 'Servo control object
    sense: "Ping Demo_NO_PST_Servo"
    ping : "ping"
    Pub Main
    pst.start(115200)
    irt.start(IRP, @code) 'after this "code" will contain the # for button pressed or 255 if no button is being pressed
    servo.start
    
    
    repeat
    pst.char(1)
    pst.dec(code)
    pst.chars(32, 3)
    waitcnt(clkfreq/100 + cnt)
    if code == (16) 'done, bail
    servo.Set(14, -250) ' P14 servo full speed counterclockwise
    servo.Set (15,250)
    elseif code == (17)
    servo.Set (14,300)
    servo.Set (15,-280)
    elseif code == (19)
    servo.Set (14, 75)
    servo.Set (15, -250)
    elseif code == (18)
    servo.Set (14, 250)
    servo.Set (15, -75) 
    else
    servo.Set(14,-10) 
    servo.Set(15, 45)
    
    Start
    
    PUB Start
    dira[LED1..LED2]~~
    outa[LED1..LED2]~
    
    waitcnt(clkfreq + cnt)
    
    repeat ' Repeat Forever
    
    range := ping.Inches(PING_Pin) ' Get Range In Inches
    ' 
    if range < Distlimit ' Comparing range to a set value of 6 inches
    outa[LED1] := ON ' P1 is on 
    outa[LED2] := OFF ' P2 is off
    servo.Set(14,250)
    servo.Set(15,-250)
    elseif range > Distlimit ' If range is further than 6 inches
    outa[LED1] := OFF ' P1 is off 
    outa[LED2] := ON ' P2 is on
    servo.Set(14,-10)
    servo.Set(15,45)
    
  • ratronicratronic Posts: 1,451
    edited 2013-07-19 18:36
    Yes you could get your ping range, check for current IR button, and set your servo's by whatever IR button is pressed or from the Ping range all inside your IR_Servo_Ping.spin program. Make sure to post your latest program archive if you have more questions after your next try.
  • ratronicratronic Posts: 1,451
    edited 2013-07-19 18:58
    This isn't tested but you can obtain the Ping range in your main loop something like this -
    _CLKMODE = XTAL1 + PLL16X                              
      _XINFREQ = 5_000_000
      
      IRP           = 2        'ir receiver i/o port pin
      PING_Pin      = 13                                          ' I/O Pin For PING)))
      LED1          = 8                                          ' I/O PIN for LED 1
      LED2          = 9                                          ' I/O PIN for LED 2
      ON            = 1
      OFF           = 0                                          
      Distlimit     = 6                                          ' In inches 
    Var
      long code
      long range
      
    Obj
      pst : "parallax serial terminal"
      irt : "irtest"
     servo: "PropBOE Servos"               'Servo control object
     sense: "Ping Demo_NO_PST_Servo"
      ping   : "ping"
    Pub Main
      pst.start(115200)
      irt.start(IRP, @code)       'after this "code" will contain the # for button pressed or 255 if no button is being pressed
      servo.start
      
        
      repeat
        pst.char(1)
        pst.dec(code)
        pst.chars(32, 3)
        waitcnt(clkfreq/100 + cnt)
        
        range := ping.inches(PING_Pin)
       
       if code == (16)                                          'done, bail
         servo.Set(14, -250)                                     ' P14 servo full speed counterclockwise
         servo.Set (15,250)    
       elseif code == (17)     
        servo.Set (14,300)     
        servo.Set (15,-280)    
       elseif code == (19)     
        servo.Set (14, 75)     
        servo.Set (15, -250)   
       elseif code == (18)     
        servo.Set (14, 250)    
        servo.Set (15, -75)    
       else                    
        servo.Set(14,-10)        
        servo.Set(15, 45)
    
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-19 19:18
    Almost there. This code works but the when using the IR Remote the servos are VERY jerky. The PING servos run fine. However, if I comment out the servo commands in the PING portion of the program the servos work fine. I am using a 9V wall wart and have the servo jumper set to vin.
  • ratronicratronic Posts: 1,451
    edited 2013-07-19 19:29
    I still have the IR setup on my PropBOE I will connect some servo's and check this out tomorrow. The Ping object is one that doesn't use another cog. By the way what programming IDE are you using and what board?
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-19 19:30
    If I comment out waitcnt(clkfreq/100 + cnt ) the IR servos run correctly but then the PING servos run jerky.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-19 20:09
    I am using the SPIN IDE on a Prop BOE Board.

    EDIT: I am flying to Montana tomorrow but will take my board, etc. with me so I can work on it. Thanks for the help.
  • ratronicratronic Posts: 1,451
    edited 2013-07-20 10:02
    After looking at your latest code you are trying to command the servo's for both far and near at the same

    time. Also the last "else" in your "if" part of your loop I imagine you will want to stop the servo's.

    Also you want to stop them when you get to close to something. I altered your code a little where the bot

    will stop when it gets to close or you are not pressing a button. I put a 3 second waitcnt but you can

    replace that with what ever you want to happen. Maybe back up a little? The choice is yours.
    Con
                                                               
      _CLKMODE = XTAL1 + PLL16X                              
      _XINFREQ = 5_000_000
      
      IRP           = 2        'ir receiver i/o port pin
      PING_Pin      = 13                                          ' I/O Pin For PING)))
      LED1          = 8                                          ' I/O PIN for LED 1
      LED2          = 9                                          ' I/O PIN for LED 2
      ON            = 1
      OFF           = 0                                          
      Distlimit     = 6                                          ' In inches 
    Var
      long code
      long range
      
    Obj
      pst  : "parallax serial terminal"
      irt  : "irtest"
     servo : "PropBOE Servos"               'Servo control object
      ping : "ping"
      
    Pub Main
      
      pst.start(115200)
      irt.start(IRP, @code)       'after this "code" will contain the # for button pressed or 255 if no button is being pressed
      servo.start
      servo.set(14, 0)
      servo.set(15, 0)
      dira[LED1..LED2]~~
      
      
      repeat
             
        if code == (16)                                         'done, bail
          servo.Set(14, -250)                                    ' P14 servo full speed counterclockwise
          servo.Set (15,250)
          
        elseif code == (17)
          servo.Set (14,200)
          servo.Set (15,-280)
          
        elseif code == (19)
          servo.Set (14, 75)
          servo.Set (15, -250)
          
        elseif code == (18)
          servo.Set (14, 250)
          servo.Set (15, -75)
           
        elseif code == 255               
          servo.Set(14, 0)       
          servo.Set(15, 0)
          '
        outa[LED1..LED2]~
        
        range := ping.Inches(PING_Pin)                      ' Get Range In Inches
      '    
        if range < Distlimit                                 ' Comparing range to a set value of 6 inches
          outa[LED1] := ON                                      ' P1 is on              
          outa[LED2] := OFF                                     ' P2 is off
          servo.Set(14, 0)
          servo.Set(15, 0)
          waitcnt(clkfreq*3 + cnt)
    
        pst.char(1)                       
        pst.str(string("IR code = "))     
        pst.dec(code)                     
        pst.chars(32, 3)                  
        pst.str(string(13, "Range   = ")) 
        pst.dec(range)                    
        pst.chars(32, 3)                  
        pst.char(13)
    
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-20 14:46
    OK. Thanks for the help. I just got to Montana and I will fire everything up and give it a shot when things settle down a bit. It might be a day or two before I can get to it. Thanks again.
  • ratronicratronic Posts: 1,451
    edited 2013-07-20 16:54
    I had never used the PropBOE Servos object. You may want to include some ramping which controls how fast it can change speed from going to one setting to another setting. It looks like the PropBoe servos object "SetUp(pin, usFromCenter,

    usStepPerPulse)" method will do what you need. You do not need to call the start method of the servo object. If you include this in place of your startup routine you will have some control of ramping and the servos will be stopped. I picked 10 us step per pulse

    but you can play with that to suit your needs.

    pst.start(115200)                                                                                                            
    irt.start(IRP, @code)       'after this "code" will contain the # for button pressed or 255 if no button is being pressed    
    servo.setup(14, 0, 10)                                                                                                       
    servo.setup(15, 0, 10)                                                                                                       
    dira[LED1..LED2]~~
    
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-20 21:26
    @ratronic, I do intend to use ramping, etc. I just wanted to get the minimal figured out first and go from there. The reason you see the -10 and 45 on the servos is because it has been a long time since I used the 2 servos I have and they need to be centered. Those are the values that actually stop them. I am hoping to have some time on Sundy to try your code out. I will let you know how it goes.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-07-22 18:10
    With a bit of modifying I was able to get your code to work properly. I had to adjust the waitcnt as it was making the servos jerky when using the IR Remote. Also, I am unable to add a > for the PING without it making the Servos jerky again. I will play with this as time permits as I really want to have it figured out before I get back to WA.
  • ratronicratronic Posts: 1,451
    edited 2013-07-22 20:33
    You will have to decide what to do once your robot hits the Distlimit. In the last code I posted I had put a 3 second waitcnt and turned the servos off. In your code since I don't know what #'s are supposed to be used for forward and backward I am guessing you are trying to

    reverse? If you do want to back up a little you will want to turn the servo's off after your waitcnt which should probably be more than the 10th of a second you have now. (Depends on what speed and how far you want it to move) Then the servo's will only move if you are

    pressing a button and it has backed up far enough to be beyond your Distlimit..
Sign In or Register to comment.