Skip to content

Commit

Permalink
Updated Arduino Serial Calls
Browse files Browse the repository at this point in the history
All files use SerialTools instead of using the Serial object directly.
  • Loading branch information
TekuConcept committed Mar 22, 2017
1 parent 615a629 commit 23d1730
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 128 deletions.
52 changes: 25 additions & 27 deletions ArduinoController/ArduinoController.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,41 @@
#include "StartController.h"
#include "PinJSON.h"

const uint8_t KILLPIN = 50; // Needs to remain global (therefore outside of JSON) for lambda interrupt
const uint8_t KILL_ADDR = 10;
const uint32_t CONTROLLER_CNT = 13u;
class IController* controllers[CONTROLLER_CNT];

void setup() {
Serial.begin(115200);
Serial.print('R');
DynamicJsonBuffer buffer;
auto json = buffer.parse(PinJSON::json);
Serial1.begin(9600);
SerialTools::writeString("Ready!", 6);

controllers[0] = new ThrustController(json["thrust"]["left_forward"].as<uint8_t>(), 50);
controllers[1] = new ThrustController(json["thrust"]["right_forward"].as<uint8_t>(), 50);
controllers[2] = new ThrustController(json["thrust"]["left_strafe"].as<uint8_t>(), 50);
controllers[3] = new ThrustController(json["thrust"]["right_strafe"].as<uint8_t>(), 50);
controllers[4] = new ThrustController(json["thrust"]["front_dive"].as<uint8_t>());
controllers[5] = new ThrustController(json["thrust"]["back_dive"].as<uint8_t>());
controllers[6] = new EscController(json["esc"].asObject());
controllers[7] = new LedController(json["led"].asObject());
controllers[8] = new PingController();
controllers[9] = new LightController(json["light"]["lights"].as<uint8_t>());
controllers[11]= new VoltageController(json["voltage"]["volt"].as<uint8_t>());
controllers[12]= new StartController();
controllers[KILL_ADDR]= new KillSwitchController(controllers, CONTROLLER_CNT, KILL_ADDR);
controllers[0]= new KillSwitchController(controllers, CONTROLLER_CNT);
attachInterrupt(
digitalPinToInterrupt(KILLPIN),
[](){((KillSwitchController*)controllers[KILL_ADDR])->isr(KILLPIN);},
digitalPinToInterrupt(KILLSWITCH_PIN),
[](){((KillSwitchController*)controllers[0])->isr(KILLSWITCH_PIN);},
CHANGE
);
while((!Serial.available())||(Serial.read()==0));
controllers[1] = new ThrustController(MOVE_PIN);
controllers[2] = new ThrustController(STRAFE_PIN);
controllers[3] = new ThrustController(DIVE_PIN);
controllers[4] = new ThrustController(YAW_PIN);
controllers[5] = new ThrustController(PITCH_PIN);
controllers[6] = new ThrustController(ROLL_PIN);
controllers[7] = new EscController();
controllers[8] = new LedController();
controllers[9] = new PingController();
controllers[10] = new LightController();
controllers[11] = new VoltageController();
controllers[12] = new StartController();

while((!Serial.available())||(Serial.read()==0));
}

void loop() {
if(Serial.available()) {
uint8_t controllerNumber = Serial.read();
if(controllerNumber < CONTROLLER_CNT)
// only execute if a command exists
controllers[controllerNumber]->execute();
uint8_t controllerIndex = SerialTools::readByte();
if(controllerIndex < CONTROLLER_CNT) {
// only execute if a command exists
DMSG("Controller: "); DMSGN(controllerIndex);
controllers[controllerIndex]->execute();
}
}
}
39 changes: 23 additions & 16 deletions ArduinoController/EscController.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#include "IController.h"

#define GPIO_CNT 6u

class EscController : public IController {

private:

uint8_t pinCount;

uint8_t* pins_;

void assignPins_(JsonArray& gpioPins) {
pins_ = new uint8_t[pinCount];
for(int i = 0; i < pinCount; i++) {
pins_[i] = gpioPins[1].as<uint8_t>();
uint8_t GPIO_PINS[GPIO_CNT] = {
ESC_S1_PIN,
ESC_S2_PIN,
ESC_S3_PIN,
ESC_S4_PIN,
ESC_S5_PIN,
ESC_S6_PIN
};
public:
EscController() {
for(uint32_t i = 0; i < GPIO_CNT; i++) {
pinMode(GPIO_PINS[i], OUTPUT);
digitalWrite(GPIO_PINS[i], HIGH);
}
}

Expand All @@ -31,16 +37,17 @@ class EscController : public IController {
}

void execute() {
while(!Serial.available());
uint8_t toggle = Serial.read();
for(int i = 0; i < pinCount; i++) {
digitalWrite(pins_[i], !toggle);
uint8_t toggle = SerialTools::readByte();
for(uint32_t i = 0; i < GPIO_CNT; i++) {
digitalWrite(GPIO_PINS[i], !toggle);
}
}

void kill() {
for(int i = 0; i < pinCount; i++) {
digitalWrite(pins_[i], HIGH);
for(uint32_t i = 0; i < GPIO_CNT; i++) {
digitalWrite(GPIO_PINS[i], HIGH);
}
}
};
};

#undef GPIO_CNT
20 changes: 9 additions & 11 deletions ArduinoController/KillSwitchController.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@ class KillSwitchController : public IController {
private:
volatile int active;
IController ** list;
int count, self;
const int STAT_LED = 22;
const int ACTIVE = 0x1;
const int INACTIVE = 0x0;
int count;

public:
KillSwitchController(IController ** _list_, int _count_, int _self_) {
KillSwitchController(IController ** _list_, int _count_) {
list = _list_;
count = _count_;
self = _self_;
pinMode(STAT_LED, OUTPUT);
pinMode(LED_STAT_PIN, OUTPUT);
}

void execute() {
Serial.println(active?'1':'0');
SerialTools::writeByte(active ? ACTIVE : INACTIVE);
}

void kill() {
for(int i = 0; i<count; i++) {
if(i != self)
list[i]->kill();
}
for(int i = 1; i < count; i++)
list[i]->kill();
}

void isr(int interrupt) {
Expand All @@ -33,7 +31,7 @@ class KillSwitchController : public IController {
active = !digitalRead(interrupt);
if(active)
kill();
digitalWrite(STAT_LED, active);
digitalWrite(LED_STAT_PIN, active);
interrupts();
}
};
17 changes: 7 additions & 10 deletions ArduinoController/LedController.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

class LedController : public IController {
private:
uint8_t green_;
uint8_t white_;
public:
LedController(JsonObject& pins) : green_(pins["green"]), white_(pins["white"]) {
pinMode(white_, OUTPUT);
digitalWrite(white_, LOW);
LedController() {
pinMode(LED_CTRL_PIN, OUTPUT);
digitalWrite(LED_CTRL_PIN, LOW);
}
void execute() {
for(int i = 0; i < 2; i++) {
digitalWrite(white_, HIGH);
digitalWrite(LED_CTRL_PIN, HIGH);
delay(250);
digitalWrite(white_, LOW);
digitalWrite(LED_CTRL_PIN, LOW);
delay(250);
}
}
void kill() {
digitalWrite(green_, LOW);
digitalWrite(white_, LOW);
digitalWrite(LED_CTRL_PIN, LOW);
}
};
};
17 changes: 6 additions & 11 deletions ArduinoController/LightController.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#include "IController.h"

class LightController : public IController {
private:
uint8_t lights_ = 45;
public:
LightController(uint8_t lights) : lights_(lights) {
pinMode(lights_, OUTPUT);
digitalWrite(lights_, HIGH);
LightController() {
pinMode(LIGHTS_PIN, OUTPUT);
digitalWrite(LIGHTS_PIN, HIGH);
}
void execute() {
while(!Serial.available());
digitalWrite(lights_, !Serial.read());
digitalWrite(LIGHTS_PIN, !SerialTools::readByte());
}
void kill() {
//digitalWrite(LIGHTS, HIGH);
}
};
void kill() { }
};
7 changes: 1 addition & 6 deletions ArduinoController/PingController.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
class PingController : public IController {
public:
void execute() {
if(Serial.available())
{
Serial.print(Serial.read());
Serial.print(" ");
}
Serial.println("I'm Here!");
SerialTools::writeString("I'm Here!", 9);
}
void kill() { }
};
10 changes: 4 additions & 6 deletions ArduinoController/StartController.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
class StartController : public IController {
private:
bool activated = false;
const int ACTIVE = 0x1;
const int INACTIVE = 0x0;

public:
void execute() {
if(activated) {
Serial.println("{\"R\":\"1\"}");
}
else {
Serial.println("{\"R\":\"0\"}");
}
SerialTools::writeByte(activated?ACTIVE:INACTIVE);
activated = false;
}
void kill() {
Expand Down
33 changes: 7 additions & 26 deletions ArduinoController/ThrustController.h
Original file line number Diff line number Diff line change
@@ -1,49 +1,30 @@
#include <Servo.h>
#include "IController.h"

uint16_t readShort() {
while(Serial.available() < 2) {}
return Serial.read() << 8 | Serial.read();
}

class ThrustController : public IController {
private:
const uint16_t IDLE = 1500;
const uint16_t IDLE = 1500;
const uint16_t FORWARD = 2000;
const uint16_t REVERSE = 1000;
int16_t tunningOffset;
Servo servo_;
public:
ThrustController(uint8_t servoPin) {
init(servoPin, 0);
}

ThrustController(uint8_t servoPin, int16_t offset) {
init(servoPin, offset);
}

void init(uint8_t servoPin, int16_t offset) {
tunningOffset = offset;
servo_.attach(servoPin);
servo_.writeMicroseconds(IDLE);
}

void execute() {
uint16_t val = readShort();
uint16_t val = SerialTools::readUShort();
servo_.writeMicroseconds(getBoundedThrottle(val));
}

void kill() {
servo_.writeMicroseconds(getBoundedThrottle(IDLE));
servo_.writeMicroseconds(IDLE);
}

uint16_t getBoundedThrottle(uint16_t value) {
uint16_t fVal = value + tunningOffset;
if(fVal > FORWARD)
return FORWARD;
else if(fVal < REVERSE)
return REVERSE;
else
return fVal;
if(value > FORWARD) return FORWARD;
else if(value < REVERSE) return REVERSE;
else return value;
}
};
};
24 changes: 9 additions & 15 deletions ArduinoController/VoltageController.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
#include "IController.h"

#define R1 30000.0
#define R2 7500.0

class VoltageController : public IController {
private:
uint8_t volt_;

const double R1 = 30000.0;
const double R2 = 7500.0;
public:
VoltageController(uint8_t volt) : volt_(volt) {
pinMode(volt_, INPUT);
VoltageController() {
pinMode(VOLT_PIN, INPUT);
}

void execute() {
int value = analogRead(volt_);
int value = analogRead(VOLT_PIN);
/*
* Measured Values for Calibration
* P1 = (11.32, 700); P2 = (8.38, 517)
*
* Solve for the linear function: x = (y-b)/m
* voltage = (2.94 * value + 13.56) / 183
*/
Serial.println((2.94 * value + 13.56) / 183);
*/
SerialTools::writeDouble((2.94 * value + 13.56) / 183);
}

void kill() {

}
};

void kill() { }
};

0 comments on commit 23d1730

Please sign in to comment.