Shop OBEX P1 Docs P2 Docs Learn Events
Advice on codes — Parallax Forums

Advice on codes

WILLQHWILLQH Posts: 11
edited 2006-01-25 18:23 in Learn with BlocklyProp
Hi !!

· I have wriiten the following code and will like to seek advice on whether they look functional and correct, before i put it on trial on my airplane.

· Basically what I need the program to do is for the parallax microprocessor to
read in 2 signals from the reciever unit (Channel 2 and 5). If channel 5
is 'Off', the processor will simply pass on the signal read in from
channel 2 to the elevator servo unmodified. On the other hand, if channel 5 is
'On', then the processor will ignore the signal read in from channel 2,
and follow its preset logic to send the necessary signal to the servo.

· Thank you !

Regards
Wilson

Comments

  • Ryan ClarkeRyan Clarke Posts: 738
    edited 2006-01-25 15:34
    As with any code, it is advisable to run tests, including making a mock circuit, or removing certain 'dangerous' factors (such as the actual flight)-

    Ryan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Ryan Clarke
    Parallax Tech Support

    RClarke@Parallax.com
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2006-01-25 18:23
    Here is some code that I wrote for an ESC (Electronic Speed Controller) with reverse and brake for an RC car.

    Towards the very beginning of the program (in the ESC section) I do a check to make sure that I am receiving
    a valid 1mS to 2mS signal from the receiver. If the signal is invalid (i.e. not there or out of range) everything
    gets turned OFF to prevent a run away car on a mindless mission with it's last known instruction! smilewinkgrin.gif

    Basically you could apply the same technique.....

    1) Read the HIGH pulse as well as the LOW pulse
    2) Add the HIGH pulse together with the LOW pulse value
    3) If the result from #2 is within the proper limits of the frame or "pulse window" from your receiver, then
    your input channel is "ON" otherwise consider it "OFF".



    'Pins
    Symbol        PWM_Pin            =    0
    Symbol        Forward_Control        =    1
    Symbol        Reverse_Control        =    2
    Symbol        Brake_Control_Pin    =    3
    Symbol        Brake_Control        =    Pin3
    Symbol        DP_CLK            =    4
    Symbol        DP_RST            =    5
    Symbol        DP_DQ_Pin        =    6
    Symbol        DP_DQ            =    Pin6
    Symbol        Signal            =    7
    
    'Constants
    Symbol        PWM_On            =    1
    Symbol        PWM_Off            =    0
    Symbol        Operating_Frequency    =    4  'MHz
    
    'Variables
    Symbol        Forward_Flag        =    Bit0 'w0.bit0
    Symbol        Reverse_Flag        =    Bit1 'w0.bit1 
    Symbol         On_Time        =    w1   
    Symbol        Off_Time        =    w2
    Symbol        Temp            =    w3
    Symbol        DP_Value        =    b8 'w4.low
    Symbol        Shift_Out        =    b9 'w4.high
    
    Output Signal
    Output Forward_Control
    Output Reverse_Control
    Output Brake_Control_Pin
    Output DP_CLK
    Output DP_RST
    Output DP_DQ_Pin
    
    OFF:
    LOW Signal
    LOW Forward_Control
    LOW Reverse_Control
    LOW Brake_Control_Pin
    LOW DP_CLK
    LOW DP_RST
    LOW DP_DQ_Pin
    DP_Value = 0
    gosub Set_DP
    
    ESC:
        pulsin PWM_Pin,PWM_Off,Off_Time
        pulsin PWM_Pin,PWM_On , On_Time
        Off_Time = Off_Time * 4 / Operating_Frequency
         On_Time = On_Time * 20 / Operating_Frequency * 2
        If Off_Time < 1000 or Off_Time > 2500 then OFF
        If  On_Time < 1000 or  On_Time > 2000 then OFF
        Temp = Off_Time + On_Time
        If Temp < 2500 or Temp > 4000 then OFF
        HIGH Signal 
        If On_Time < 1250 then FWD
        If On_Time < 1500 then REVERSE_BRAKE
        IF On_Time < 1750 then FORWARD_BRAKE
    REV:
        Reverse_Flag = 1
        Forward_Flag = 0
        DP_Value = On_Time - 1750
        Brake_Control = 0
            LOW Forward_Control
            gosub Set_DP
            HIGH Reverse_Control
        goto ESC
    
    FWD:
        Reverse_Flag = 0
        Forward_Flag = 1
        DP_Value = 1250 - On_Time
        Brake_Control=0
        LOW Reverse_Control
            gosub Set_DP
            HIGH Forward_Control
        goto ESC
    
    REVERSE_BRAKE:
        DP_Value = 1500 - On_Time * Reverse_Flag
            DP_Value = DP_Value * 2 max 250
        LOW Reverse_Control
            LOW Forward_Control
        Brake_Control=DP_Value max 1
            gosub Set_DP
        goto ESC
    
    FORWARD_BRAKE:
        DP_Value = On_Time - 1500 * Forward_Flag
            DP_Value = DP_Value * 2 max 250
            LOW Reverse_Control
            LOW Forward_Control
        Brake_Control = DP_Value max 1
            gosub Set_DP
        goto ESC
    
    Set_DP:
        DP_Value = DP_Value * 51 / 50    
        HIGH DP_RST
        DP_DQ = 0
        Temp = 20 / Operating_Frequency
        for Shift_Out = 1 to 9
                PULSOUT DP_CLK,Temp
        next    
        for Shift_Out = 1 to 8
                DP_DQ = DP_Value / 128
                PULSOUT DP_CLK,Temp
                DP_Value=DP_Value * 2
        next
        LOW DP_RST
    return
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
Sign In or Register to comment.