Shop OBEX P1 Docs P2 Docs Learn Events
Unsure how to approach — Parallax Forums

Unsure how to approach

Jack3Jack3 Posts: 55
edited 2014-02-03 12:45 in Propeller 1
Well I have searched and searched and cannot find out how to proceed. With Spin, I have no idea at all, with C, I wonder if pulse_in(pin, state) might be my answer. It returns a long that is the duration of the "pulse in" but I need it to look for 3 states, not just 2

My quest is a light controller for RC aircraft. One function that is desirable is to be able to turn on/off the landing lights from the transmitter and also have them do a flash pattern in one mode. This is usually accomplished on the RC transmitter with a 3 position switch. I can write the code after I know how to ingest the state/change in state, of that signal that comes off of the RC receiver. (Spectrum or XPS 2.4ghz systems). I plan on having a cog run a loop looking for change of state and acting upon it

I guess I don't know what to expect incoming or how to have values for the 3 states so I can do my truth's to get the state/change of state. The receiver system runs either analog or digital servos.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-01-31 14:19
    This is a piece of cake for the Propeller.

    I modified an object which reads 6 RC channels to make it easier to use on any of the Propeller's pins. You don't have to use all six channels. It includes a demo program.

    Let say the you store the channel of interest in variable "x". You could do some thing like this.
     if x > 1700
        ' turn on red light
      elseif x > 1300
        ' turn on green light
      else
         ' turn off lights
    

    I'm just guessing on the pulse values and what you would want to do (turn on or off lights).

    I've done stuff like this myself in many of my projects.
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-01-31 15:05
    Here's how you can monitor and measure a pulse using Spin running in anther cog.
    pri monitor_pulse(pin, p_value) | ustix, mask                   ' launch with cognew
    
    '' Monitors high-going pulse on pin
    '' -- (long) values stored at hub address in p_value
    
      ustix := clkfreq / 1_000_000                                  ' ticks in 1us
      mask  := 1 << pin
    
      dira[pin] := 0                                                ' set to input
    
      frqa := 1
      ctra := (%01000 << 26) | pin                                  ' pos detect mode
      waitpne(mask, mask, 0)                                        ' wait for idle       
      
      repeat
        phsa := 0                                                   ' clear accumulator
        waitpeq(mask, mask, 0)                                      ' wait for pulse
        waitpne(mask, mask, 0)                                      ' let it finish
        long[p_value] := phsa / ustix                               ' write to hub (us)
    


    You need declare an array of longs to serve as the stack for this cog; 16 is plenty. In your mainline code, start the cog like this:
    cognew(monitor_pulse(PIN, @pulsewidth), @stack)
    


    where PIN is the IO pin receiving the pulse (use a resistor if this is coming from a 5v system), and pulsewidth is a global variable that will hold the pulse. This lets you keep the logic of dealing with the pulse width in the main code (my preference as other factors could affect behavior).
  • Jack3Jack3 Posts: 55
    edited 2014-01-31 15:43
    I shall ingest both of these further. I grasp the C better than the Spin, possibly because I am more familiar (as best as a rookie can be) with the C. I still have much to learn. Very, very weak in any math stuff. Takes me a long time to understand stuff because of that. Once I can step it out ....

    I appreciate the rapid responses. I did download the RCreceiver121116c.spin and have given that enough of a look to know I need to digest it....but it seems simpler than the one you modified from, as I had already looked and that and could not follow it on first attempts.

    I saved JphnnyhMac's too.
  • Jack3Jack3 Posts: 55
    edited 2014-02-01 09:04
    Jon,
    I think that is more what I am looking for, but I need to keep on with the educational stuff until I get that far along. Need to finish chapter 7 in the BOE first and hope that answers my need to clearly understand what the H is going on. Part of my problem was I had no idea what would come from the RC system until I spoke with a friend this morning and got that cleared up. Therefore, I think what you have offered is more to what I am trying to do.

    Duane, cool stuff, but way further than I want to go with a simple light controller.

    So to clarify:
    I finally learned that I need to read a square wave and check for changes in state to 3 possible ranges and act accordingly
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-02-01 10:42
    Just be mindful that some "educational" materials will show you the "easy" way to do things, which -- from a professional point-of-view -- is not always the best way to do things.

    Using the code I posted above, you could do something like this in your main loop:
    main | t
    
      cognew(monitor_pulse(RC_PIN, @pulsewidth), @stack)
    
      t := cnt
      repeat
        case pulsewidth
          1250..1350:
            green_light
          1650..1750:
            red_light
          other:
            lights_off
    
        ' other loop code
    
        waitcnt(t += (clkfreq / 10))                                ' run loop 10x second
    


    I find case a little cleaner than if-else in when checking for a value in multiple ranges. Systems will vary, so giving your code a little bit of latitude around the expected values is helpful.
  • jazzedjazzed Posts: 11,803
    edited 2014-02-01 10:58
    Jack3 wrote: »
    Well I have searched and searched and cannot find out how to proceed. With Spin, I have no idea at all, with C, I wonder if pulse_in(pin, state) might be my answer. It returns a long that is the duration of the "pulse in" but I need it to look for 3 states, not just 2

    Hello Jack,

    Please edit your post here to point back to this thread or delete it. It can be confusing to have 2 threads on the same topic. Moderators will remove it if you like.

    I looked for more info on the XPS 2.4ghz receiver, and found these pages:

    http://www.xtremepowersystems.net/data/RFU_v1.0.pdf
    http://www.xtremepowersystems.net/data/X10+1.3.pdf

    Assuming for now that is within the ball park, I'll try to offer a solution. A servo is usually controlled by a PWM signal ... which is basically a rectangular wave.

    If I'm not mistaken, your PWM control signal may look something like below. I can't find anything in the docs that prove it though, and this post could be a waste of time.

    So, assuming your 3 states are actually referring to a PWM, it may mean the first state is the start (low-high) transition on the left side of the active control time pulse rather than the level of the pulse. The second state would be the (high-low) transition on the active control time pulse. The third state would be the next start state (low-high) transition.

    So it seems what you really want to measure are the 2 periods : the active control time and the passive wait time. This can be easily handled by pulse_in(pin, state).

    If the servo connector (like the X10+1.3 doc) is connected to say P12 on an ActivityBoard, then one can say:
    #include "simpletools.h"
    
    int active_ustime = 0;
    int passive_ustime = 0;
    
    void somefunction()
    {
    // assume the pattern repeats, so we wait 100ms just to make sure we get the passive time accurately.
    pause(100);   // [URL="https://propsideworkspace.googlecode.com/hg/Learn/Simple Libraries/Utility/libsimpletools/html/simpletools_8h.html#a381e2d58c3d6a1f0fd8129bcc4726804"]pause() documentation[/URL]
    active_ustime = pulse_in(12, 1); // [URL="https://propsideworkspace.googlecode.com/hg/Learn/Simple Libraries/Utility/libsimpletools/html/simpletools_8h.html#ae251e0a94359068f5e56682311ef4201"]pulse_in() documentation[/URL]
    pause(100);   // [URL="https://propsideworkspace.googlecode.com/hg/Learn/Simple Libraries/Utility/libsimpletools/html/simpletools_8h.html#a381e2d58c3d6a1f0fd8129bcc4726804"]pause() documentation[/URL]
    passive_ustime = pulse_in(12, 0);
    }
    
    Red and blue text is mine. Picture comes from the servo doc.

    attachment.php?attachmentid=106646&d=1391279656
    939 x 511 - 44K
  • Jack3Jack3 Posts: 55
    edited 2014-02-01 16:26
    Jon, Jazzed,
    Sorry about not linking the two posts. I did ask about pulse_in in the first post here and because I got no response to a "C question" I assumed I needed to post over in the C part.

    Anyhow, I learned that I needed to look for a pulsewidth and also that it had to be a range and was hoping the ... notation would do the trick for that. The links to the documentation are awesome, thanks. Still trying to learn how to get to those places. So much to learn, please be patient.

    Using the picture and labeling what each is helps me visualize too.
  • jazzedjazzed Posts: 11,803
    edited 2014-02-02 11:24
    Jack3 wrote: »
    Jon, Jazzed,
    Sorry about not linking the two posts. I did ask about pulse_in in the first post here and because I got no response to a "C question" I assumed I needed to post over in the C part.

    Anyhow, I learned that I needed to look for a pulsewidth and also that it had to be a range and was hoping the ... notation would do the trick for that. The links to the documentation are awesome, thanks. Still trying to learn how to get to those places. So much to learn, please be patient.

    Using the picture and labeling what each is helps me visualize too.
    Sorry that no one saw your "C question."

    To find those pages in SimpleIDE, look at the Help Menu, and click on Simple Library Reference. The online version of Simple Libraries can be found here.

    We have considered adding an alphabetical index, but just making it a priority has been a little rough. Maybe we can start that back up in mid February.

    We all had to swear an oath of patience here especially for all our new friends :) Old friends sometimes fight like cats and dogs or crabbyistas though.
  • Jack3Jack3 Posts: 55
    edited 2014-02-03 12:45
    Jon,
    I think I will probably follow your suggestions, but need to finish chapter 7 so I learn about the cog registers, which has been started.
Sign In or Register to comment.