32 Replies Latest reply on Mar 26, 2020 9:20 PM by colporteur

    Helicopter animation using a Raspberry Pi

    colporteur

      We are being encouraged by our leaders to practice social distancing and/or isolate ourselves where possible during this COVID19 crisis. To avoid going stir crazy while isolated in my home, I dug up an old project I have mulled over many times.

       

      I would like to use a Pi to construct an helicopter animation. The best way to understand the goal of the project is let you take a look at the video. https://www.youtube.com/watch?v=Hl5--7g3jO0&t=112s

       

      My first task is to build the light sequence. My light sequence is slightly different because it matches aviation specifications. There will be cabin lighting, landing lighting(front), position lighting (left/right/rear) and strobe lighting (top/bottom). All lights are either on or off with the strobes flashing when on. The lighting is being done with LEDs. Once I master the lights, I will integrate the audio and then tackle motor control. The Pi is responsible for running the whole show. I am looking to use python to accomplish the task. No, I am not interested in doing the project with an Arduino or in another programming language. Pi and python are my toolset. Hopefully I don't discover it is not possible.

       

      What I am hoping for is some direction to developing the solution. I have no doubt this is programming 101 for many of you. I have much experience in programming. I have met and worked with people that have the skills to call themselves a programming. I am not a programmer.

       

      The series of LED’s repeats the following sequence:

      turn LED1 on solid

      turn LED2 on flashing

      turn LED3 on solid

      turn LED4 on solid

      turn LED4 off

      turn LED3 off

      turn LED2 off

      turn LED1 off

      cycle starts again.

       

      My difficultly is creating the flashing LED within the sequence. I can turn all the LED's on and off. I can flash LED2 on its own. When I add flashing to the sequence LED2 flashes once and then the sequence continues with no more flashes. I require LED2 to flash continuous until it is turned off. I’m thinking I need to have a LED flashing routine (? another script) that is called in the parent script and continues to run (flash) until it is called to stop. I want to call it a fire and forget routine but that is my lack of programming terminology understanding.

       

      I had this thought, write a code for a python module that flashes an led. Call the module in another python script giving it the pin number. The module should have a start and stop. Well that simple understanding of writing a module exceeds my knowledge. Since I'm not sure if what I am looking for is actually a solution, my research is proving difficult. If a module is what I need and you have a resource to assist in writing it, please let me know.

       

      I'm not looking for someone to write the code but rather guide me to developing the code myself. Care to provide some insight into my helicopter animation COVID19 Isolation Project. There is no badge or points awarded, just a brain share. I look forward to your responses.

        • Re: Helicopter animation using a Raspberry Pi
          dougw

          You could have a loop that runs at the flashing rate.

          Each time through the loop decide which LEDs and functions are active or inactive based on the loop count.

          Generally only the flashing LED will change each time through the loop, but after a certain number of loops each of the other functions will change state.

          The flashing LED may flash between a start count and an end count.

          1 of 1 people found this helpful
            • Re: Helicopter animation using a Raspberry Pi
              colporteur

              Thanks Doug for the reply.

               

              Help me understand you defintion of a loop? I have code that causes flashing. When I insert that code it runs once because my understanding of python is it is sequential. Step 1, step 2, step 3 and so forth. If step 2 is a loop that runs continious and the code advances to step 3 then a loop is what I think will work.

               

              I thought step2 as a function but that didn't work. I thought step2 as a called module but that didn't work. Now forgive me when I say module. I import another python script. I have been trying to understand class objects hoping that might be the solution.

               

              Can you provide a loop reference? When I research loops, i get the basic if then else return or while true.

                • Re: Helicopter animation using a Raspberry Pi
                  dougw

                  Here is a description of loops in Python:

                  https://www.learnpython.org/en/Loops

                  Assume your whole sequence takes the same time as 100 flashes, you would have a while loop that runs while count < 100

                  Inside the loop you can turn the flashing LED on if it is off else turn it off

                  If you want to turn cabin lights on at a count of 10 it is just another if statement inside the loop like if count > 10 turn on cabin led

                  if statement syntax can be found here:

                  ttps://www.tutorialspoint.com/python/python_if_else.htm

                    • Re: Helicopter animation using a Raspberry Pi
                      colporteur

                      OK we are both on the same page when it comes to understanding loops. I wanted to make sure your loop and my loop were the same before proceeding with the discussion.

                       

                      The turning on and then off of all lights is eight events. Four on and four offs. One of those events is a strobe. When the strobe is on, it is flashing. The whole sequence will occur over a specified time period. The whole eight event sequence is a loop.  That timing of the full loop will need to be refined as I integrate rotor animation and the sound. I'm estimating approx 2 minutes.

                       

                      If the strobe was at the end of the sequence then I can see a loop working. Turn all three lights on, run the another loop to generate the strobe and then proceed to turn all three lights off. Restart sequence. Since the strobe is in the middle of turning on the lights, it needs to be a fire and forget. My thought is to have a start loop routine, then proceed with the other events and during the off sequence a stop loop routine.

                       

                      I have been unable to separate the strobe to run independent from the other events. While the strobe loop is running the other two lights remain off. At least the way I have developed the code so far.

                       

                      while True:

                         #helicopter lights on sequence,cab_lit,stb_lit,pos_lit,lnd_lit
                         GPIO.setup(cab_lit, GPIO.HIGH)

                        time.sleep(3)

                        objled_test.start() <--- this piece of the puzzle (strobe) needs to continue to run as the sequence of events continues. The code is an example of a failed attempt.

                        GPIO.setup(pos_lit, GPIO.HIGH)

                        time.sleep(3)

                        GPIO.setup(lnd_lit, GPIO.HIGH)

                        time.sleep(3)

                         # helicopter lights off sequence,lnd_lit,pos_lit,stb_lit,cab_lit
                         GPIO.setup(lnd_lit, GPIO.LOW)

                        time.sleep(3)

                        GPIO.setup(pos_lit, GPIO.LOW)

                        time.sleep(3)

                        objled_test.stop()

                        GPIO.setup(cab_lit, GPIO.LOW)

                        time.sleep(3)

                       

                      Intergrated into this will be sound and motor control. I broke the animation into three components, light sound and motors. I'm working the light part now and add the others after that is equipped.

                        • Re: Helicopter animation using a Raspberry Pi
                          dougw

                          To calculate the number of loops in your while statement:

                          Suppose your strobe flashes 2 times per second (when it is flashing). In 2 minutes it would have flashed 240 times.

                          If it toggles on or off in each loop you would need 480 loops, with a 0.25 second delay at the end of each loop to make the 480 loops last 2 minutes.

                           

                          Suppose you want the strobe to be flashing for 10 seconds at the 1 minute mark and off the rest of the time.

                          If the loop count is > 240 and < 280 then toggle the strobe LED on or off on each count else turn the strobe LED off

                           

                          Suppose you want the cabin lights on after 10 seconds and off after 100 seconds.

                          If the count is > 40 and < 400 turn the cabin LEDs on else turn them off.

                           

                          For any of/off event you just choose at which count it needs to happen and use an if statement to make it happen, even the strobe flashing on and off events..

                          1 of 1 people found this helpful
                          • Re: Helicopter animation using a Raspberry Pi
                            luislabmo

                            Hi Sean,

                             

                            I think dougw 's idea is on spot and is the easiest to do without having to use timers, threads or a complex secs/micros/millis calculation. The key here is deciding the delay for each loop (a delay common enough for all actions to be a "factor" of such delay) and be creative on the math of how many loops you want and how you want to handle each action. e.g.:

                             

                            The "roug" example below will do the following

                            • Blink ON or OFF a GREEN_LED, 1 second each state
                            • Blink ON or OFF a RED_LED, 5 seconds each state
                            • Engine -the following repeats every 100 seconds:
                              • Seconds 0 to 19, the ENGINE is OFF
                              • Seconds 20 to 34, the ENGINE will start accelerating
                              • Seconds 35 to 80, the ENGINE will turn ON at full speed
                              • Second 90 to 99, the ENGINE is OFF
                            • The GREEN_LED and RED_LED sequence will act independently (doesn't matter what the engine sequence is doing)

                             

                            #Initialization
                            cycleCounter = 0
                            totalCycles = 100
                            #RED_LED OFF
                            #GREEN_LED OFF
                            #ENGINE OFF
                            
                            while True:
                                 if cycleCounter % 2 == 0:
                                      #Turn GREEN_LED ON
                                 else:
                                      #Turn GREEN_LED OFF
                            
                                 if (cycleCounter // 5) % 2 == 0:
                                      #Turn RED_LED ON
                                 else:
                                      #Turn RED_LED OFF
                            
                                 if cycleCounter >= 20:
                                      #Turn ENGINE ON acceleration
                                 elif cycleCounter >= 35:
                                      #Turn ENGINE ON FULL SPEED
                                 elif cycleCounter >= 90:
                                      #Turn ENGINE OFF
                            
                                 cycleCounter = (cycleCounter + 1) % totalCycles
                                 time.sleep(1)     
                            

                             

                            Luis

                            2 of 2 people found this helpful
                              • Re: Helicopter animation using a Raspberry Pi
                                colporteur

                                Thank you for chiming in.

                                 

                                I've been struggling to get my head around dougw suggestion. Does my thinking align with this paraphrase of the method: all actions of LEDs, sound and motors are governed by the value of a counter. Each action required tests the value of the counter if true/false of test perform action (on/off, start/stop). The counter just cycles.

                                 

                                How do I get granular control of the strobe light on & off time.  My current line of thinking this code

                                def flashHZ (GPIO1):

                                  GPIO.output(GPIO1, GPIO.HIGH)  ## set HIGH (LED ON)
                                   time.sleep(.05## wait
                                   GPIO.output(GPIO1, GPIO.LOW)  ## set LOW (LED OFF)
                                   time.sleep(.8## wait
                                   return

                                gives me the optimal flash.

                                 

                                Break.Break...

                                How did you display you code so cleanly in your post? The numbering and ability to scroll within the message is ideal. Can you share your insight?

                                1 of 1 people found this helpful
                                  • Re: Helicopter animation using a Raspberry Pi
                                    luislabmo

                                    Hi Sean,

                                     

                                    The granularity of the strobe as you describe will work but it has a flaw. While flashHZ is being executed the rest of the code will be on hold. If you want the program to do something else at the same time it won't work as you expect it (you'll need threading for that). The problem with threading is, it may help you accomplish what you need but eventually the threads may be out of sync as threading is asynchronous (the Pi will be executing other tasks at the same time) so they won't execute precisely on a given amount of time, also threading may be more complex to manage.

                                    Going back to your example, the idea is to find a common factor to all the actions that will easily help you calculate intervals for all your actions. From your example, the shortest action requires 1/20 seconds (0.05), that may be your delay for each iteration, and every other action will be a factor of thas number, see below:

                                     

                                    #Initialization  
                                    cycleCounter = 0  
                                    totalCycles = 16 # A factor of 16 (16, 32, 48, 64, etc) so the cycle starts over
                                    GPIO.output(GPIO1, GPIO.LOW)  
                                      
                                    while True:  
                                      if cycleCounter % 16 == 0:
                                        GPIO.output(GPIO1, GPIO.HIGH)
                                      else:  # 0.05 * 16 = 0.8 (the delay required while off)
                                        GPIO.output(GPIO1, GPIO.LOW)
                                    
                                      cycleCounter = (cycleCounter + 1) % totalCycles  
                                      time.sleep(.05)
                                    

                                     

                                    And, to display the code I used the Syntax Highlight and used Tab to align it (spaces work too)

                                     

                                    Luis

                                    2 of 2 people found this helpful
                                      • Re: Helicopter animation using a Raspberry Pi
                                        colporteur

                                        G'Day,

                                        I have been concentrating on the threading option following some of glennvanderveer suggestions. I speculating this is not a complicated program so have a thread run for 30secs and then stopping it might be doable. Now if I could just get the thread to stop:)

                                         

                                        Thanks for the cut and paste on inserting code. I will definitely use the feature. Making it easy for readers to view code is one step to improving the chances of getting an answer.

                          • Re: Helicopter animation using a Raspberry Pi
                            colporteur

                            Programming folks I have been put onto something called concurrency through threading or multiprocessing. I will continue to read but would appreciate someone pulling me back from the edge if I am heading in the wrong direction.

                            • Re: Helicopter animation using a Raspberry Pi
                              glennvanderveer

                              What I think you need for flashing led2 is a timer routine.  In theory, when you wanted the LED2 to start flashing, you would clear a counter(if needed), and start the timer.  When the time expires (this could be you flashing rate) the timer routine would toggle the LED2 GPIO pin, reset the counter(if using), and exit.  Then when you wanted to stop the LED2 flashing, you would just have to stop the timer.  No threading or multiprocessing needed...

                              1 of 1 people found this helpful
                                • Re: Helicopter animation using a Raspberry Pi
                                  colporteur

                                  Thank for your input.

                                   

                                  I have developed a routine that results in a LED2 flash that simulates a strobe. Varying the on and off times gives me the strobe effect I was looking for. When the routine is called, using my programming skills, the strobes either flash but the other LED's don't change or all the LEDs light up and nothing after that.

                                   

                                  House  isolation gives me the time to keep programming. Keep the suggestions coming.

                                    • Re: Helicopter animation using a Raspberry Pi
                                      glennvanderveer

                                      Try looking here for hints on how to use a timer:  https://www.geeksforgeeks.org/timer-objects-python/

                                       

                                      Is this what you want to do:

                                       

                                      LED1 on

                                      LED2 start flashing

                                      LED3 on, LED2 keep flashing

                                      LED4 on, LED2 keep flashing

                                      <time passess>LED2 keep flashing

                                      LED4 off, LED2 keep flashing

                                      LED3 off, LED2 keep flashing

                                      LED2 off, LED2 stop flashing

                                      LED1 off, LED2 still off

                                      repeat...

                                      1 of 1 people found this helpful
                                        • Re: Helicopter animation using a Raspberry Pi
                                          balearicdynamics

                                          Probably, this is the most elegant and efficient solution. The possible alternative I see is managing interrupts chained so every state set to a different state in the others but this is tricky and not interesting in this case. A real alternative, instead, at least in my opinion, is creating a dictionary in Python collecting every change in a different boolean variable. Then a single function will set/reset the LEDs on/off as well as the motor, sound, and any other part that should be included in the animation. This optimizes the code and makes easier to change the pattern of the animation.

                                          In the main infinite cycle while true if using a json file with different animation patterns these can be randomly looped to get a more interesting effect.

                                           

                                          Enrico

                                            • Re: Helicopter animation using a Raspberry Pi
                                              colporteur

                                              Sorry M, you're going to have to dummy down your response for me to process. I'm the dummy:) I'm inputting at the rate of a paper tape reader punch and your outputting at the rate of SCSI 2 interface.

                                               

                                              I see some benefit to the concept of "Then a single function will set/reset the LEDs on/off as well as the motor, sound, and any other part that should be included in the animation." Understand the goal is to integrate the three components of animation, lights, sound and motions is important.  Unfortunately, I don't have the knowledge level to extrapolate what I need to do to accomplish this.

                                               

                                              I'm stuck on the simple concept of concurrency. Two tasks operating independently. On and off LED's and LED strobe on and LED strobe off. How do I do that?

                                                • Re: Helicopter animation using a Raspberry Pi
                                                  dougw

                                                  Your problem could be handled with concurrent processes and it seems simple enough that you have a few things that might be independently occurring, but when you dive into implementing concurrency in a computer, it rapidly becomes a very complex topic. I doubt it could be distilled down to a paragraph of advice that would avoid a big learning curve.

                                                  If you want to describe your problem a bit more theoretically than just the mechanics of loops, you could look into state machines. This problem can be described as a system that has different states at different times and the states follow a sequence. At any given time some, things are on and others are off - that defines the state of the system at that time. The loop counter we mentioned above is a way to implement a state machine. Every quarter second the system enters a new state. You just need to decide what needs to be on and what needs to be off in each quarter second.

                                              • Re: Helicopter animation using a Raspberry Pi
                                                colporteur

                                                Yes V Squared the additional commentary in the sequence is correct! Your additional commentary enhances to detail of what I am trying to accomplish.

                                                 

                                                The pilots are in the helicopter running through a startup checklist. More than one thing happen at the same time. Lights will come on solid and some will flash. engines will start later generating sound and motor will engage rotors to turn.. The viewer on the outside just sees lights, hears engines and witnesses motion of the blades.

                                                 

                                                My goal is to create this sequence using a Pi running python.

                                                  • Re: Helicopter animation using a Raspberry Pi
                                                    glennvanderveer

                                                    So this is what I whipped up quickly.  I have not tested or compiled this, but it works great in my head...

                                                    import threading
                                                    import RPi.GPIO as GPIO
                                                    
                                                    
                                                    def LED2_Blinker():
                                                    if GPIO.input (LED2_PIN): #read teh led2 pin and if it is high, set it low
                                                    GPIO.output (LED2_PIN, GPIO.LOW)
                                                    else: #was not high, set it so...
                                                    GPIO.output (LED2_PIN,GPIO.HIGH)
                                                    
                                                    LED1_PIN = XX #change XX to be proper pin number on PI
                                                    LED2_PIN = XX #change XX to be proper pin number on PI
                                                    LED3_PIN = XX #change XX to be proper pin number on PI
                                                    LED4_PIN = XX #change XX to be proper pin number on PI
                                                    
                                                    
                                                    GPIO.setup (LED1_PIN, GPIO.OUT)
                                                    GPIO.setup (LED2_PIN, GPIO.OUT)
                                                    GPIO.setup (LED3_PIN, GPIO.OUT)
                                                    GPIO.setup (LED4_PIN, GPIO.OUT)
                                                    
                                                    
                                                    #set all pins low to start....
                                                    GPIO.output (LED1_PIN,GPIO.LOW)
                                                    GPIO.output (LED2_PIN,GPIO.LOW)
                                                    GPIO.output (LED3_PIN,GPIO.LOW)
                                                    GPIO.output (LED4_PIN,GPIO.LOW)
                                                    
                                                    
                                                    #setup the timer.  Number is for amount of time between timer activating...
                                                    timer = threading.Timer(0.25,LED2_Blinker)
                                                    
                                                    
                                                    # and now the looping sequence...
                                                    while True:
                                                    GPIO.output (LED1_PIN,GPIO.HIGH)
                                                    time.sleep(3)
                                                    timer.start() #starts the timer so that LED 2 will blink automagically
                                                    GPIO.output (LED3_PIN,GPIO.HIGH)
                                                    time.sleep(3)
                                                    GPIO.output (LED4_PIN,GPIO.HIGH)
                                                    time.sleep(3)
                                                    #done startup sequence...
                                                    GPIO.output (LED4_PIN,GPIO.LOW)
                                                    time.sleep(3)
                                                    GPIO.output (LED3_PIN,GPIO.LOW)
                                                    time.sleep(3)
                                                    timer.cancel() #stops the tiemr from firing until restarted...
                                                    GPIO.output (LED2_PIN,GPIO.LOW) #ensure that LED2 is off (just in case)
                                                    GPIO.output (LED1_PIN,GPIO.LOW)
                                                    time.sleep(3)
                                                    

                                                     

                                                    This should repeat forever, until you power it off...

                                                    1 of 1 people found this helpful
                                            • Re: Helicopter animation using a Raspberry Pi
                                              colporteur

                                              I'm posting the code produced so far. It produces the following error message on run.

                                               

                                              pi@piheli:~/scripts $ python light_sequence.py

                                              cabin lights on

                                              strobe lights on

                                              light_sequence.py:114: RuntimeWarning: No channels have been set up yet - nothing to clean up!  Try cleaning up at the end of your program instead!

                                                GPIO.cleanup()

                                               

                                              The intent of the code is to thread the strobe light in a class that would provide a start and terminate method. I have spent a good six hours today, reading and trying different iterations of code to resolve the issue with no success. I indicated in my original post, I wasn't looking for someone to provide the answer. I'm wondering if I am close to a solution or I may be way off the mark.

                                               

                                              I lack not only the programming skills but the experience to troubleshoot. Can anyone see what this is missing or is the approach not sound and I will consider starting over.

                                               

                                              #!/usr/bin/python
                                              #
                                              ###########
                                              #
                                              #Description:light_sequence.py-script to start and stop four series of lights
                                              # on helicopter simulation
                                              #Date: Mar.18,2020
                                              #Revision:1.0
                                              #
                                              ###########
                                              
                                              
                                              import time # provides various time-related functions
                                              import RPi.GPIO as GPIO # provides interface to input/output pins of the Pi
                                              import random
                                              import os
                                              import threading            # provide concurrency between static and flashing LED
                                              
                                              
                                              
                                              
                                              # setup GPIO
                                              #GPIO.setwarnings(False)
                                              GPIO.setmode(GPIO.BOARD)           # set option to reference physical pins
                                              
                                              
                                              # GPIO pin definitions
                                              lnd_lit = 3  # Define GPIO pin
                                              cab_lit = 5  # Define GPIO pin
                                              pos_lit = 7  # Define GPIO pin
                                              IO_5 = 29 # Define GPIO pin
                                              IO_6 = 31  # Define GPIO pin
                                              IO_7 = 26  # Define GPIO pin
                                              IO_8 = 24  # Define GPIO pin
                                              IO_9 = 21  # Define GPIO pin
                                              IO_10 = 19  # Define GPIO pin
                                              IO_11 = 23  # Define GPIO pin
                                              IO_12 = 32  # Define GPIO pin
                                              IO_13 = 33  # Define GPIO pin
                                              IO_14 = 8  # Define GPIO pin
                                              IO_15 = 10  # Define GPIO pin
                                              IO_16 = 36  # Define GPIO pin
                                              IO_17 = 11  # Define GPIO pin
                                              IO_18 = 12  # Define GPIO pin
                                              IO_19 = 35  # Define GPIO pin
                                              IO_20 = 38  # Define GPIO pin
                                              IO_21 = 40  # Define GPIO pin
                                              IO_22 = 15  # Define GPIO pin
                                              IO_23 = 16  # Define GPIO pin
                                              IO_24 = 18  # Define GPIO pin
                                              IO_25 = 22  # Define GPIO pin
                                              IO_26 = 37  # Define GPIO pin
                                              stb_lit = 13  # Define GPIO pin
                                              
                                              
                                              #GPIO pins in use for lights
                                              Heli_lit_array =(
                                                  cab_lit, pos_lit, lnd_lit
                                              )
                                              
                                              
                                              # Set GPIO function to output
                                              for light in Heli_lit_array:
                                                  GPIO.setup(light, GPIO.OUT)  # set GPIO pins for lighting output
                                              
                                              
                                              class StrobeOnOffTask:
                                                  def __init__(strobe, pin):
                                                      strobe._running = True
                                                      strobe.pin = pin
                                                      print(strobe.pin)
                                                      GPIO.setmode(GPIO.BOARD)
                                                      GPIO.setup(strobe.pin, GPIO.OUT)
                                              
                                              
                                              def terminate(strobe):
                                                  strobe._running = False
                                              
                                              
                                              def run(strobe):
                                                  while strobe._running:
                                                      GPIO.output(strobe.pin, GPIO.HIGH)  ## set HIGH (LED ON)
                                                      time.sleep(.05)  ## wait
                                                      GPIO.output(strobe.pin, GPIO.LOW)  ## set LOW (LED OFF)
                                                      time.sleep(.8)  ## wait
                                              
                                              
                                              try:
                                                      while True:
                                                          #helicopter lights on sequence,cab_lit,stb_lit,pos_lit,lnd_lit
                                                          print("cabin lights on")
                                                          GPIO.setup(cab_lit, GPIO.HIGH)
                                                          time.sleep(3)
                                                          print("strobe lights on")
                                                          c = StrobeOnOffTask()
                                                          t = threading.Thread(target = c.run, args = [stb_lit])
                                                          t.start()
                                                          time.sleep(3)
                                                          print("position lights on")
                                                          GPIO.setup(pos_lit, GPIO.HIGH)
                                                          time.sleep(3)
                                                          print("landing lights on")
                                                          GPIO.setup(lnd_lit, GPIO.HIGH)
                                                          time.sleep(3)
                                                          # helicopter lights off sequence,lnd_lit,pos_lit,stb_lit,cab_lit
                                                          print("landing lights off")
                                                          GPIO.setup(lnd_lit, GPIO.LOW)
                                                          time.sleep(3)
                                                          print("position lights off")
                                                          GPIO.setup(pos_lit, GPIO.LOW)
                                                          time.sleep(3)
                                                          print("strobe lights off")
                                                          c.terminate()
                                                          t.join()
                                                          print("cabin lights off")
                                                          GPIO.setup(cab_lit, GPIO.LOW)
                                                          time.sleep(3)
                                              
                                              
                                              except KeyboardInterrupt:  # if ctrl+c pressed exit cleanly
                                                  GPIO.cleanup()
                                              except:  # this catches ALL other exceptions including errors.
                                                  GPIO.cleanup()
                                              finally:  # cleanup GPIO on normal exit
                                                  GPIO.cleanup()
                                              
                                              1 of 1 people found this helpful
                                                • Re: Helicopter animation using a Raspberry Pi
                                                  glennvanderveer

                                                  Okay here we go with alpha .02 of my code.  Again, this works gloriously in my head, but I have not tested it or even compiled it...

                                                   

                                                  import threading  
                                                  import RPi.GPIO as GPIO  
                                                      
                                                  def LED2_Blinker(): 
                                                  if BlinkFlag:
                                                       if GPIO.input (LED2_PIN): #read teh led2 pin and if it is high, set it low  
                                                          GPIO.output (LED2_PIN, GPIO.LOW)  
                                                      else: #was not high, set it so...  
                                                      GPIO.output (LED2_PIN,GPIO.HIGH)  
                                                    
                                                  LED1_PIN = XX #change XX to be proper pin number on PI  
                                                  LED2_PIN = XX #change XX to be proper pin number on PI  
                                                  LED3_PIN = XX #change XX to be proper pin number on PI  
                                                  LED4_PIN = XX #change XX to be proper pin number on PI  
                                                  
                                                  BlinkFlag=0    #make sure when timer starts, the led doesn't start blinking before it should  
                                                    
                                                  GPIO.setup (LED1_PIN, GPIO.OUT)  
                                                  GPIO.setup (LED2_PIN, GPIO.OUT)  
                                                  GPIO.setup (LED3_PIN, GPIO.OUT)  
                                                  GPIO.setup (LED4_PIN, GPIO.OUT)  
                                                      
                                                  #set all pins low to start....  
                                                  GPIO.output (LED1_PIN,GPIO.LOW)  
                                                  GPIO.output (LED2_PIN,GPIO.LOW)  
                                                  GPIO.output (LED3_PIN,GPIO.LOW)  
                                                  GPIO.output (LED4_PIN,GPIO.LOW)  
                                                      
                                                  #setup the timer.  Number is for amount of time between timer activating...  
                                                  timer = threading.Timer(0.25,LED2_Blinker)  
                                                      
                                                  # and now the looping sequence...  
                                                  while True:  
                                                  GPIO.output (LED1_PIN,GPIO.HIGH)  
                                                  time.sleep(3)  
                                                  #timer.start() #starts the timer so that LED 2 will blink automagically  
                                                  BlinkFlag=1
                                                  GPIO.output (LED3_PIN,GPIO.HIGH)  
                                                  time.sleep(3)  
                                                  GPIO.output (LED4_PIN,GPIO.HIGH)  
                                                  time.sleep(3)  
                                                  #done startup sequence...  
                                                  GPIO.output (LED4_PIN,GPIO.LOW)  
                                                  time.sleep(3)  
                                                  GPIO.output (LED3_PIN,GPIO.LOW)  
                                                  time.sleep(3)  
                                                  #timer.cancel() #stops the tiemr from firing until restarted...  
                                                  BlinkFlag = 0
                                                  GPIO.output (LED2_PIN,GPIO.LOW) #ensure that LED2 is off (just in case)  
                                                  GPIO.output (LED1_PIN,GPIO.LOW)  
                                                  time.sleep(3) 
                                                  

                                                   

                                                  Not much of a change from alpha .01, but this eliminates the timer from biting you in the arse because it never needs to be canceled.  All that needs to be done when you want to start LED2 blinking, just set BlinkFlag to 1.  When you don't want the LED to blink, set BlinkFlag to 0.  When the timer fires and it runs Led2_Blinker, it first checks to see if BlinkFlag is set to non zero.  If true, toggle the LED.  Other wise just exit and wait for next timer firing.

                                                    • Re: Helicopter animation using a Raspberry Pi
                                                      colporteur

                                                      Thanks V2 for generating some code. After some tweaks to get it running the following.

                                                       

                                                      No blink on start, reguired a timer.join() to get it back at cancel. Second pass the following error.

                                                       

                                                      Traceback (most recent call last):

                                                        File "light_sequence.py", line 39, in <module>

                                                          timer.start() #starts the timer so that LED 2 will blink automagically

                                                        File "/usr/lib/python2.7/threading.py", line 730, in start

                                                          raise RuntimeError("threads can only be started once")

                                                      RuntimeError: threads can only be started once

                                                        • Re: Helicopter animation using a Raspberry Pi
                                                          glennvanderveer

                                                          Crap, so now we are both getting bitten in the arse...  Stupid computers...  Doing exactly what I tell them to do but never what I really want them to do.  You would think that after all this time working as a programmer, I would be able to blink a simple LED when I want...

                                                          Give me a bit of time and I will come up with something that I will actually compile and do some basic testing on and then I will post it here for you...

                                                            • Re: Helicopter animation using a Raspberry Pi
                                                              colporteur

                                                              I appreciate your assistance. I have an a semblance of code that generates the lights and the motor. It uses the gpiozero model suggested by beacon_dave . My last piece of the puzzle is audio using this code. It needs some cleanup to look pretty but I can generate light and rotor motion.

                                                               

                                                              #!/usr/bin/python3
                                                              #
                                                              ###########
                                                              #
                                                              #Description:light_sequence.py-script to start and stop four series of lights
                                                              # on helicopter simulation using GPIOZero to replace RPi.GPIO
                                                              #Date: Mar.23,2020
                                                              #Revision:2.0
                                                              #
                                                              ###########
                                                              
                                                              
                                                              import time # provides various time-related functions
                                                              from gpiozero import LED, PWMLED # provides interface to input/output pins of the Pi
                                                              import random
                                                              import os
                                                              
                                                              
                                                              # setup GPIO
                                                              landing_light = LED(2)
                                                              cabin_light = LED(3)
                                                              position_light = LED(4)
                                                              strobe_light = PWMLED(27, initial_value = 0.1, frequency = 100)
                                                              motor_direction = LED(20)
                                                              motor_step = LED(21)
                                                              
                                                              
                                                              
                                                              
                                                              while True:
                                                                  #helicopter lights on sequence,cab_lit,stb_lit,pos_lit,lnd_lit
                                                                  print("cabin lights on")
                                                                  cabin_light.on()
                                                                  time.sleep(3)
                                                                  print("strobe lights on")
                                                                  strobe_light.pulse()
                                                                  time.sleep(3)
                                                                  print("position lights on")
                                                                  position_light.on()
                                                                  time.sleep(3)
                                                                  print("landing lights on")
                                                                  landing_light.on()
                                                                  time.sleep(3)
                                                                  print("rotor start")
                                                                  motor_step.blink(on_time=0.01, off_time=0.01)
                                                                  time.sleep(10)
                                                                  print("rotor stop")
                                                                  motor_step.off()
                                                                  # helicopter lights off sequence,lnd_lit,pos_lit,stb_lit,cab_lit
                                                                  print("landing lights off")
                                                                  landing_light.off()
                                                                  time.sleep(3)
                                                                  print("position lights off")
                                                                  position_light.off()
                                                                  time.sleep(3)
                                                                  print("strobe lights off")
                                                                  strobe_light.off()
                                                                  time.sleep(3)
                                                                  print("cabin lights off")
                                                                  cabin_light.off()
                                                                  time.sleep(3)