...
- Handshake
- Receive multiple Objects (one at a time)
- ...
Connect to a flight controller
Image Added
Mainport and Flexiport can be used to connect an arduino to and make use of serial communication.
Wire | Port | Arduino |
---|
Black | Ground | Ground |
Red | VCC | 5V |
Blue | Tx | Rx |
Orange | Rx | Tx |
Examples
Simple Example
Code Block |
---|
language | cpp |
---|
theme | Emacs |
---|
title | Simple Example |
---|
linenumbers | true |
---|
collapse | true |
---|
|
//Library to use pins as serial port
#include <SoftwareSerial.h>
//Include a librepilot header file
#include "flightstatus.h"
//Include the LibrePilotSerial Library
#include "LibrePilotSerial.h"
//Initialize serial port
SoftwareSerial mySerial(2, 3); // RX, TX
//Initialize LibrePilot serial connection
LibrePilotSerial lps(&mySerial);
void setup() {
//Begin LibrePilotSerial communication
lps.serial->begin(57600);
}
void loop() {
//Request object from FC
lps.request(FLIGHTSTATUS_OBJID);
//Receive object from FC. This function will block until the specified object was received or it times out.
//It returns true if a valid packet was received
//the packet is stored in the array of the object packet union
boolean ok = lps.receive(FLIGHTSTATUS_OBJID, FlightStatusDataUnion.arr, 200);
//the packet data may only be accessed if the return value was true
if(ok) {
//the packet can be accessed in a structured manner via the data member
if (FlightStatusDataUnion.data.Armed == FLIGHTSTATUS_ARMED_ARMED) {
//the quad is armed, do something!
}
}
delay(250); //wait 250 ms
} |
...
Curcuit:
(See on curcuits.io for BOM)
Sketch:
Code Block |
---|
language | cpp |
---|
theme | Emacs |
---|
firstline | 1 |
---|
title | CustomArmingLED |
---|
linenumbers | true |
---|
collapse | true |
---|
|
#include <SoftwareSerial.h>
#include "FlightStatus.h"
#include "LibrePilotSerial.h"
SoftwareSerial mySerial(2, 3); // RX, TX
LibrePilotSerial lps(&mySerial);
int ledPinr = 9;
int ledPing = 8;
int ledPinb = 7;
void setup() {
// initialize serial monitor:
Serial.begin(57600);
//initialize LibrePilotSerial Object
lps.serial->begin(57600);
}
void loop() {
lps.request(FLIGHTSTATUS_OBJID); //request object
//watch the telemetry stream for 200ms.
//Store object if it is received and return true. If not, return false.
boolean ok = lps.receive(FLIGHTSTATUS_OBJID, FlightStatusDataUnion.arr, 200);
Serial.print(" Result fs: ");
Serial.println(FlightStatusDataUnion.data.Armed);
if(ok) {
if(FlightStatusDataUnion.data.Armed == FLIGHTSTATUS_ARMED_DISARMED) {
analogWrite(ledPinr, 0);
analogWrite(ledPing, 255);
analogWrite(ledPinb, 0);
} else if (FlightStatusDataUnion.data.Armed == FLIGHTSTATUS_ARMED_ARMING) {
analogWrite(ledPinr, 0);
analogWrite(ledPing, 0);
analogWrite(ledPinb, 255);
} else if (FlightStatusDataUnion.data.Armed == FLIGHTSTATUS_ARMED_ARMED) {
analogWrite(ledPinr, 255);
analogWrite(ledPing, 0);
analogWrite(ledPinb, 0);
}
}
delay(500);
} |
...