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
13 changes: 9 additions & 4 deletions src/hand-controller/buttons.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "buttons.h"
#include "handController.h"
#include <MicroBit.h>

// Button variables:
int throttle = 0;
bool armed = false;
static int throttle = 0;
static bool armed = false;

const int THROTTLE_THRESHOLD = 40;
const int THROTTLE_MIN = 5;
Expand Down Expand Up @@ -31,7 +32,7 @@ static void onButtonAB(MicroBitEvent) {
// uBit.display.print(armed ? "1" : "0");
}

void setBtnsHandlers() {
void SetBtnsHandlers() {
uBit.messageBus.listen(DEVICE_ID_BUTTON_A, DEVICE_BUTTON_EVT_CLICK,
onButtonA);
uBit.messageBus.listen(DEVICE_ID_BUTTON_B, DEVICE_BUTTON_EVT_CLICK,
Expand All @@ -40,7 +41,11 @@ void setBtnsHandlers() {
onButtonAB);
}

void checkPanic() {
int GetThrottle() { return throttle; }

bool IsArmed() { return armed; }

void CheckPanic() {
if (uBit.accelerometer.getGesture() == ACCELEROMETER_EVT_SHAKE) {
armed = false;
throttle = 0;
Expand Down
7 changes: 7 additions & 0 deletions src/hand-controller/buttons.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

void SetBtnsHandlers();
int GetThrottle();
bool IsArmed();

void CheckPanic();
23 changes: 5 additions & 18 deletions src/hand-controller/handController.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#include "buttons.h"
#include "orientation.h"
#include "pins.h"
#include "transmitter.h"
#include "view.h"
#include <MicroBit.h>

#ifndef HAND_CONTROLLER_H
Expand All @@ -7,26 +12,8 @@

#ifdef MICROBIT_UBIT_AS_STATIC_OBJECT
extern MicroBit uBit; // Target the same uBit as in main.cpp

// Hand controller variables, PARTY
extern int pitch;
extern bool armed;
extern int roll;
extern int throttle;
extern int yaw;
#else
extern MicroBit &uBit;
#endif

// Setup peripherals:
void setBtnsHandlers();
void setP1High();

// Monitoring:
void setOrientation();
void updateDisplay();
void checkPanic();
void transmittData();
void readPins();

#endif
14 changes: 9 additions & 5 deletions src/hand-controller/orientation.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "orientation.h"
#include "handController.h"
#include <MicroBit.h>
#include <algorithm>

// orientation variables:
int pitch = 0;
int roll = 0;
static int pitch = 0;
static int roll = 0;

const int ANGULAR_THRESHOLD = 45;
const int STEADY_STATE_THRESHOLD = 15;
Expand All @@ -27,7 +27,7 @@ static int clampAngles(int value, int threshold) {
return clampedValue;
}

void setOrientation() {
void SetOrientation() {
int measuredPitch = uBit.accelerometer.getPitch();
pitch = clampAngles(measuredPitch, ANGULAR_THRESHOLD);

Expand All @@ -36,4 +36,8 @@ void setOrientation() {

// uBit.display.scroll(pitch);
// uBit.display.scroll(roll);
}
}

int GetPitch() { return pitch; }

int GetRoll() { return roll; }
6 changes: 6 additions & 0 deletions src/hand-controller/orientation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once
#include <algorithm>

void SetOrientation();
int GetPitch();
int GetRoll();
11 changes: 7 additions & 4 deletions src/hand-controller/pins.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "pins.h"
#include "handController.h"
#include <MicroBit.h>

int yaw = 0;
static int yaw = 0;

// ADC has a resolution of 1 Byte
// There are different thresholds for the two pins, as P0 is more sensible
Expand Down Expand Up @@ -33,15 +34,15 @@ static int setYawFromP2Read(int meas) {
}
}

void setP1High() {
void SetP1High() {
int succ = uBit.io.P1.setAnalogValue(MAX_PWM_VALUE);
if (succ != DEVICE_OK) {
uBit.display.scroll("P1");
uBit.sleep(1000);
}
}

void readPins() {
void ReadPins() {
int p0 = uBit.io.P0.getAnalogValue();
int p2 = uBit.io.P2.getAnalogValue();

Expand All @@ -54,4 +55,6 @@ void readPins() {
} else if (p2 > p0) {
yaw = setYawFromP2Read(p2);
}
}
}

int GetYaw() { return yaw; }
5 changes: 5 additions & 0 deletions src/hand-controller/pins.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

void SetP1High();
void ReadPins();
int GetYaw();
16 changes: 10 additions & 6 deletions src/hand-controller/transmitter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include "transmitter.h"
#include "buttons.h"
#include "handController.h"
#include "orientation.h"
#include "pins.h"
#include <MicroBit.h>

/*
Expand Down Expand Up @@ -52,10 +56,10 @@ static void sendValue(char name, int value) {
uBit.radio.datagram.send(buf, i);
}

void transmittData() {
sendValue('P', pitch);
sendValue('A', armed);
sendValue('R', roll);
sendValue('T', throttle);
sendValue('Y', yaw);
void TransmittData() {
sendValue('P', GetPitch());
sendValue('A', IsArmed());
sendValue('R', GetRoll());
sendValue('T', GetThrottle());
sendValue('Y', GetYaw());
}
3 changes: 3 additions & 0 deletions src/hand-controller/transmitter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void TransmittData();
16 changes: 10 additions & 6 deletions src/hand-controller/display.cpp → src/hand-controller/view.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "view.h"
#include "buttons.h"
#include "handController.h"
#include "orientation.h"
#include "pins.h"
#include <MicroBit.h>
#include <cmath>

// MicroBit display
const uint16_t LED_DISPLAY_SIZE = 5;
Expand All @@ -19,7 +22,7 @@ static void updatePulse() {
}

static void displayArmed(Image &ledDisplay) {
if (armed) {
if (IsArmed()) {
updatePulse();
ledDisplay.setPixelValue(0, 0, armedPulse);
} else {
Expand All @@ -28,6 +31,7 @@ static void displayArmed(Image &ledDisplay) {
}

static void displayThrottle(Image &ledDisplay) {
int throttle = GetThrottle();
if (throttle == 0) {
return;
} else {
Expand All @@ -41,17 +45,17 @@ static uint16_t angleToDisplayCoord(int angle) {
}

static void displayPitchRoll(Image &ledDisplay) {
uint16_t x = angleToDisplayCoord(roll);
uint16_t y = angleToDisplayCoord(pitch);
uint16_t x = angleToDisplayCoord(GetRoll());
uint16_t y = angleToDisplayCoord(GetPitch());
ledDisplay.setPixelValue(x, y, LED_ON);
}

static void displayYaw(Image &ledDisplay) {
uint16_t x = static_cast<uint16_t>(yaw / 30 + 2);
uint16_t x = static_cast<uint16_t>(GetYaw() / 30 + 2);
ledDisplay.setPixelValue(x, 0, LED_ON);
}

void updateDisplay() {
void UpdateDisplay() {
uBit.display.clear();

Image ledDisplay = Image(LED_DISPLAY_SIZE, LED_DISPLAY_SIZE);
Expand Down
4 changes: 4 additions & 0 deletions src/hand-controller/view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <cmath>

void UpdateDisplay();
16 changes: 8 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ static void FlightController() {
}

static void HandController() {
setBtnsHandlers();
setP1High();
SetBtnsHandlers();
SetP1High();

while (true) {
setOrientation();
updateDisplay();
checkPanic();
transmittData();
readPins();
SetOrientation();
UpdateDisplay();
CheckPanic();
TransmittData();
ReadPins();
uBit.sleep(100);
}
}

int main() {
uBit.init();
initRadio();
FlightController();
HandController();
return 0;
}
Loading