Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

struct RadioPacketChars {
char Armed;
char Pitch;
char Roll;
char Throttle;
char Yaw;

constexpr RadioPacketChars()
: Armed('A'), Pitch('P'), Roll('R'), Throttle('T'), Yaw('Y') {}
};

constexpr RadioPacketChars DefaultRadioPacketChars{};
3 changes: 2 additions & 1 deletion src/flight-controller/flightController.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "battery.h"
#include "receiver.h"
#include "servoController.h"
#include "ultrasonicSensor.h"
#include "view.h"
Expand All @@ -15,7 +16,7 @@

#define MICROBIT_UBIT_AS_STATIC_OBJECT

enum class State : uint16_t { CALIBRATING, CHARGING, DISARMED, ARMED, PANIC };
enum class State : uint8_t { CALIBRATING, CHARGING, DISARMED, ARMED, PANIC };

struct FlightState {
int altitude;
Expand Down
46 changes: 46 additions & 0 deletions src/flight-controller/receiver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "receiver.h"
#include "../constants.h"
#include "flightController.h"

#include <MicroBit.h>

static RadioPacketChars radioPacketChars = DefaultRadioPacketChars;
const uint8_t PACKET_CHAR_INDEX = 14;
const uint8_t PACKET_VALUE_INDEX = 9;

static uint8_t packet[32];

// Reference variables
static int pitch = 0;
static int roll = 0;
static int throttle = 0;
static int yaw = 0;
static bool armed = false;

static void readRadioPacket() {
uBit.radio.datagram.recv(packet, sizeof(packet));
char packetChar = packet[PACKET_CHAR_INDEX];

if (packetChar == radioPacketChars.Pitch) {
memcpy(&pitch, &packet[PACKET_VALUE_INDEX], 4);
} else if (packetChar == radioPacketChars.Armed) {
int32_t value = 0;
memcpy(&value, &packet[PACKET_VALUE_INDEX], 4);
armed = value != 0;
} else if (packetChar == radioPacketChars.Roll) {
memcpy(&roll, &packet[PACKET_VALUE_INDEX], 4);
} else if (packetChar == radioPacketChars.Throttle) {
memcpy(&throttle, &packet[PACKET_VALUE_INDEX], 4);
} else if (packetChar == radioPacketChars.Yaw) {
memcpy(&yaw, &packet[PACKET_VALUE_INDEX], 4);
}
}

void FlushRadioBuffer() {
// sizeof returns the byte size of struct
for (unsigned int i = 0; i < sizeof(radioPacketChars); i++) {
readRadioPacket();
}
}

bool IsDroneArmed() { return armed; }
4 changes: 4 additions & 0 deletions src/flight-controller/receiver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

bool IsDroneArmed();
void FlushRadioBuffer();
3 changes: 3 additions & 0 deletions src/flight-controller/view.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "view.h"
#include "../utilities.h"
#include "flightController.h"
#include "receiver.h"
#include <MicroBit.h>
#include <cmath>

Expand Down Expand Up @@ -44,5 +46,6 @@ void UpdateView() {

Image ledDisplay = Image(LED_DISPLAY_SIZE, LED_DISPLAY_SIZE);
viewBatteryLevel(ledDisplay);
DisplayArmed(IsDroneArmed(), ledDisplay);
uBit.display.print(ledDisplay);
}
17 changes: 17 additions & 0 deletions src/hand-controller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,20 @@ $$

![img/display.jpg](../../img/display.jpg)

#### Radio communication

We use a radio channel to send packets between the hand controller and flight controller. These packets contains control variables and setpoints.

Packet Spec per byte:
| 0 | 1 ... 4 | 5 ... 8 | 9 ... 28
----------------------------------------------------------------
| packet type | system time | serial number | payload

- Packet type = 1 is the first byte
- The following four bytes (int size) is the system time
- The following four bytes (int size) is the serial number, which is set to 0

The Payload is divided in two:
- The first four payload bytes (9, 10, 11, 12) is the integer value sent
- The following payload byte (13) is the name length
- The following payload byte (14) is the identifying char ('P', 'A', 'R', 'T' 'Y')
20 changes: 12 additions & 8 deletions src/hand-controller/transmitter.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "transmitter.h"
#include "../constants.h"
#include "buttons.h"
#include "handController.h"
#include "orientation.h"
#include "pins.h"
#include <MicroBit.h>

static RadioPacketChars radioPacketChars = DefaultRadioPacketChars;

/*
// Packet Spec:
// | 0 | 1 ... 4 | 5 ... 8 | 9 ... 28
Expand All @@ -30,7 +33,7 @@ static void sendValue(char name, int value) {
uint8_t buf[32];
int i = 0;

// Packet type value
// Packet type value (first byte)
buf[i++] = 0x01;

// Timestamp (4 bytes)
Expand All @@ -48,18 +51,19 @@ static void sendValue(char name, int value) {
i += 4;

// Name length
buf[i++] = 1;
memcpy(&buf[i], &name, 1);
buf[i++] = 1; // 13th byte
// Name
memcpy(&buf[i], &name, 1); // 14th byte
i += 1;

// Send the packet
uBit.radio.datagram.send(buf, i);
}

void TransmittData() {
sendValue('P', GetPitch());
sendValue('A', IsArmed());
sendValue('R', GetRoll());
sendValue('T', GetThrottle());
sendValue('Y', GetYaw());
sendValue(radioPacketChars.Pitch, GetPitch());
sendValue(radioPacketChars.Armed, IsArmed());
sendValue(radioPacketChars.Roll, GetRoll());
sendValue(radioPacketChars.Throttle, GetThrottle());
sendValue(radioPacketChars.Yaw, GetYaw());
}
24 changes: 2 additions & 22 deletions src/hand-controller/view.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "view.h"
#include "../utilities.h"
#include "buttons.h"
#include "handController.h"
#include "orientation.h"
Expand All @@ -8,27 +9,6 @@
// MicroBit display
const uint16_t LED_DISPLAY_SIZE = 5;
const int LED_ON = 255;
int pulseDirection = 15;
int armedPulse = 0;

static void updatePulse() {
armedPulse += pulseDirection;

if (armedPulse == 255) {
pulseDirection = -15;
} else if (armedPulse == 0) {
pulseDirection = 15;
}
}

static void displayArmed(Image &ledDisplay) {
if (IsArmed()) {
updatePulse();
ledDisplay.setPixelValue(0, 0, armedPulse);
} else {
armedPulse = 0;
}
}

static void displayThrottle(Image &ledDisplay) {
int throttle = GetThrottle();
Expand Down Expand Up @@ -59,7 +39,7 @@ void UpdateDisplay() {
uBit.display.clear();

Image ledDisplay = Image(LED_DISPLAY_SIZE, LED_DISPLAY_SIZE);
displayArmed(ledDisplay);
DisplayArmed(IsArmed(), ledDisplay);
displayThrottle(ledDisplay);
displayPitchRoll(ledDisplay);
displayYaw(ledDisplay);
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ static void FlightController() {

while (true) {
SetBatteryInfo();
FlushRadioBuffer();
UpdateView();
uBit.sleep(1000);
uBit.sleep(100);
}
}

Expand All @@ -47,6 +48,6 @@ static void HandController() {
int main() {
uBit.init();
initRadio();
HandController();
FlightController();
return 0;
}
23 changes: 23 additions & 0 deletions src/utilites.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "utilities.h"

static int pulseDirection = 15;
static int armedPulse = 0;

static void updatePulse() {
armedPulse += pulseDirection;

if (armedPulse == 255) {
pulseDirection = -15;
} else if (armedPulse == 0) {
pulseDirection = 15;
}
}

void DisplayArmed(bool isArmed, Image &ledDisplay) {
if (isArmed) {
updatePulse();
ledDisplay.setPixelValue(0, 0, armedPulse);
} else {
armedPulse = 0;
}
}
4 changes: 4 additions & 0 deletions src/utilities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <MicroBit.h>

void DisplayArmed(bool isArmed, Image &ledDisplay);
Loading