I want to know if I can have a python program I’m running on my laptop talk to Mission planner (running on the same laptop) to tell it when to turn on/off a GPIO pin (or change PWM, either is fine) on my cube orange.
Background: I’ve built a quadcopter and am flying auto missions over the fields at my farm. I’ve got a cube orange, here3 GPS, and downward facing lidar. I’m able to use the local LEICA network to get RTK float and pretty precise lines over the fields. I use terrain following in auto mode and remain about 1m above the field. I added a fpv camera and am sending the video back to my laptop where I use tensorflow to identify if the plants under the quadcopter are crops or weeds - we have a lot of thorny weeds growing amongst the switchgrass. So far, so good, these forums have been invaluable in getting me this far, and it’s been a fun project.
What I’d like to do next is add a small tank/diaphragm pump/sprayer nozzle to the quadcopter and only turn it on when it’s flying over a weed. Python/Tensorflow is running on my laptop and can identify the weeds, I’m wondering if it can somehow send an “on” or “off” signal from that python program to Mission Planner which can be sent over MAVLink and then show up as “on” or “off” on a GPIO output of my cube orange. From there I can switch on/off the diaphragm pump to only spray the weeds.
I’ve been looking through docs and haven’t found what I’m looking for yet, I’d appreciate pointers in the right direction.
If I’m reading that correctly it’d have to be added to the mission while I’m creating it, but I don’t know when to add it (when to turn on/off relay pins) until the copter is actually flying over that section of ground - it’s sending back images of the ground below it which I’m doing image recognition on in tensorflow in near real-time. If tensorflow identifies a weed (vs healthy desired plants in the field) then I want to send a command to turn on the relay (which turns on a sprayer) to spray only that weed. Is there a way to send a command like that ad hoc when it’s needed rather than adding it to the planned mission?
Thanks for the pointers. I found pymavlink, MAVSDK-Python and DroneKit-Python projects and documentation. I think any of them can do what I need. I’ll read up and get this worked into my project.
At a quick glance they seem like different implementations of python interfaces to MAVLink. In your experience is any better than another?
For future readers, here’s how to turn on/off relay 0 (AUX5). I attached a multimeter so I could see the pin switch between 3.3V and 0V. First you install modules: python -m pip install --user --upgrade pymavlink pyserial
Next update the code below for you connection (I was on USB/com6, 115k), then run it. You should see the relay pin go high, then low.
@Yuri_Rage - The issue I now face is that I must disconnect mission planner from the copter in order to connect this script. I found I couldn’t have both connected on com6 which makes sense (for now I’m on USB, I usually use RFD 900x telemetry). Is there a way around that? Or should I just load up the waypoints file, set it to auto, arm it, disconnect (all in mission planner), then connect my pymavlink script to turn on/off the relay?
You can write a very similar script to run within Mission Planner on its scripting tab or you can mirror the MAVLink connection by pressing Ctrl-F and using the button at the bottom of this screenshot:
I’m using python3 tensorflow functions and I think the python that runs in the mission planner scripts tab is python2, but I might be able to get the two python instances talking using sockets or multiprocessing.
But your suggestion of mirroring the mavlink stream is cool. I’ll look into that. Thanks for all your help!