CircuitFlyer / Touch_and_Go

Basic electric control line model aircraft timer using an Adafruit Trinket M0 & CircuitPython
MIT License
4 stars 4 forks source link

Love it! Still need a little help with a slightly different use case #2

Closed Wiliest-Hiker closed 2 years ago

Wiliest-Hiker commented 2 years ago

So I came across your "Touch and Go" project and I love it. Really appreciate the work and the share. Anyway, I want to use the Trinket m0 for the Outlaw pinewood derby race coming up this winter. Brushless motor with an ESC running to a ducted fan. Some of your code will be very helpful to me, but I have questions. I used to code but it's been 20 years so I'm painfully rusty.

What I'm trying to do is use the Infrared sensor from Adafruit to sense the start pin and use that to kick the motor into high gear. 1) initial mode, unbroken beam, car at rest. The motor will be at zero and the sensor will be waiting for the beam to be broken 2) the IR beam is broken by the starting pin and it spins the motor at a relatively slow speed (say, 10% max). If the beam becomes unbroken again it will stop spinning. Such would be the case with a finger passing the beam, for example. 3) if the beam stays broken for a couple seconds, the car "arms" itself so that when the beam becomes unbroken it will go into step 4 4) when the pin drops it fires the motor on high for about 1.5 seconds and then shuts down. This only happens if the beam was broken for a couple seconds first - as a safety mechanism. 5) car needs to be reset to get back into initial mode (step 1)

I've gotten the board to recognize the IR beam and to change colors depending on whether it's broken or not but I'm not 100% sure how to connect/run the ESC from the board. I think I can learn all that from your current program. However, any idea on how I build the timed portions into the program?

CircuitFlyer commented 2 years ago

That sound like a fun project. Adafruit Learn has lots of great tutorials and example code for circuitpython. I suggest https://learn.adafruit.com/welcome-to-circuitpython and https://learn.adafruit.com/using-servos-with-circuitpython/overview

The ESC uses a standard RC servo signal. Just treat the ESC like a special servo where a 0 angle is minimum throttle (off) and 180 angle is max throttle (full speed). Any angle in between is a proportional throttle setting. In my code, instead of using servo.angle I used servo.fraction. Which is the same idea but instead of an angle 0 to 180 I use a decimal fraction from 0.0 to 1.0.

The Touch_and_Go code is a little more than what you would need so if you are able to weed out the unnecessary stuff it should be a good example to use for your idea. At its base, the code is a classic "State Machine", although I called them modes in my code. At any given time only 1 state is active. If you want more info see https://learn.adafruit.com/circuitpython-101-state-machines. Break your sequence into finite states and then determine the task to be performed while in that state. Next, determine the conditions to test that will trigger the transition from one state to another.

Something like this:

The top part of the program loads the library modules, sets up the IR sensor, sets up the servo and outputs minimum throttle (off) and defines some variables (including the initial state, let's call it READY).

The main loop is run through over and over without any delay or waiting as fast as possible, about 1000 times a second. At the start of each run through the main loop record the current time.

State = READY: Task: Nothing to do here, except maybe set the Dotstar to green to give a visual indication of what's going on. Transition: If beam is broken then State = SAFETY. One extra thing here, the next step uses time so record the time at this exact point to be used later.

State = SAFETY: Task: Nothing to do here either, except set the Dotstar to white so you know whats happening. Transition: Two things here. 1) If beam is not broken then State = READY (go back and start over). 2) If beam is still broken AND the time since the last transition is greater than 2 seconds then State = ARM.

State = ARM: Task: Set the throttle to 10% and turn the Dotstar yellow. Transition: If beam is not broken then State = RUN. Again, the next state uses time so record the time at this point.

State = RUN: Task: Set the throttle to full speed and turn the Dotstar to red. Transition: If the time since the last transition is greater then 1.5 seconds then State = END

State = END: Task: Set the throttle to minimum (off) and turn the Dotstar blue. Transition: There is no transition here. You want the program to conclude and stop running by entering an infinite loop where the only exit is the reset button.

Study my code and you will see I have done something similar to this. I hope this helps. Good luck.

Wiliest-Hiker commented 2 years ago

Thanks so much. I'll definitely check into all of it and see how I do. I'll update you!

Wiliest-Hiker commented 2 years ago

Hey, Just wanted you to know that I got it partially working last year, but with work schedules never got to finish it. Working on it again this year, hoping to put on a good show come February.

-- Michael Croteau http://www.linkedin.com/in/croteau-michael

On Thu, Sep 8, 2022 at 11:12 AM CircuitFlyer @.***> wrote:

Closed #2 https://github.com/CircuitFlyer/Touch_and_Go/issues/2 as completed.

— Reply to this email directly, view it on GitHub https://github.com/CircuitFlyer/Touch_and_Go/issues/2#event-7348823782, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWTJZ5N33GNNOJK25EBRWXLV5H67JANCNFSM5IPRPOTQ . You are receiving this because you authored the thread.Message ID: @.***>