Skip to content

Commit

Permalink
Adds firetruck sim exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
nosknut committed Aug 6, 2023
1 parent 21d1dcc commit 9edffc9
Show file tree
Hide file tree
Showing 31 changed files with 1,216 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Exercises/FiretruckSim/Boom/Boom.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "FiretruckSim.h"

const int BOOM_UP_PICKER_PIN = 15;
const int BOOM_DOWN_PICKER_PIN = 16;
const int BOOM_ELEVATION_PIN = 18;

const int BOOM_LEFT_PICKER_PIN = 19;
const int BOOM_RIGHT_PICKER_PIN = 20;
const int BOOM_ROTATION_PIN = 22;

const int BOOM_ELBOW_OUT_PICKER_PIN = 23;
const int BOOM_ELBOW_IN_PICKER_PIN = 24;
const int BOOM_ELBOW_PIN = 25;

const int BOOM_WRIST_PIN = 28;

void setup()
{
Serial.begin(9600);
}

void loop()
{
Serial.println("Hello World!");
delay(1000);
}
189 changes: 189 additions & 0 deletions Exercises/FiretruckSim/Boom/FiretruckSim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#include <Arduino.h>
#include <ArduinoJson.h>

// All the pinMode payloads that have
// been sent to the simulator will be stored here.
// If the simulator is reset, it will request the pinModes
// and this string will be used.
String pinModes = "";

byte pinValues[100];

void syncPinsWithSim()
{
while (Serial.available() > 0)
{
DynamicJsonDocument jsonDoc(500);
DeserializationError error = deserializeJson(jsonDoc, Serial);

if (error != DeserializationError::Ok)
{
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}

if (jsonDoc.containsKey("p"))
{
int pin = jsonDoc["p"].as<int>();
int value = jsonDoc["v"].as<int>();
pinValues[pin] = value;
}

if (jsonDoc.containsKey("init"))
{
// Send the pinModes to the simulator
Serial.print(pinModes);

// Parse all the json messages in the pinModes string
// and send the pin values for all outputs
// to the simulator.

String buffer = pinModes;
while (buffer.length() > 0)
{
DynamicJsonDocument jsonDoc(50);
DeserializationError error = deserializeJson(jsonDoc, buffer);

// Remove the json message from the buffer
buffer = buffer.substring(buffer.indexOf("}") + 1);

if (error != DeserializationError::Ok)
{
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
continue;
}

if (jsonDoc.containsKey("m"))
{
int pin = jsonDoc["p"].as<int>();
String mode = jsonDoc["m"].as<String>();

if (mode == "o")
{
// Send the pin value to the simulator
DynamicJsonDocument jsonDoc(100);
jsonDoc["p"] = pin;
jsonDoc["v"] = pinValues[pin];
serializeJson(jsonDoc, Serial);
}
}
}
}
}
}

int digitalReadSim(uint8_t pin)
{
syncPinsWithSim();
return pinValues[pin];
}

int analogReadSim(uint8_t pin)
{
syncPinsWithSim();
return pinValues[pin];
}

void analogWriteSim(uint8_t pin, int val)
{
syncPinsWithSim();

if (val == pinValues[pin])
{
return;
}

pinValues[pin] = val;

DynamicJsonDocument jsonDoc(50);

jsonDoc["p"] = pin;
jsonDoc["v"] = val;

serializeJson(jsonDoc, Serial);
syncPinsWithSim();
}

void digitalWriteSim(uint8_t pin, uint8_t val)
{
analogWriteSim(pin, val);
}

void pinModeSim(uint8_t pin, uint8_t mode)
{
syncPinsWithSim();

DynamicJsonDocument jsonDoc(50);

jsonDoc["m"] = (mode == OUTPUT) ? "o" : "i";
jsonDoc["p"] = pin;

serializeJson(jsonDoc, Serial);

// Store the payload in a string for later use
serializeJson(jsonDoc, pinModes);

syncPinsWithSim();
}

#define digitalRead digitalReadSim
#define analogRead analogReadSim
#define digitalWrite digitalWriteSim
#define analogWrite analogWriteSim

#define pinMode pinModeSim

size_t printSim(const String &s)
{
syncPinsWithSim();

DynamicJsonDocument jsonDoc(500);
jsonDoc["print"] = s;

serializeJson(jsonDoc, Serial);
}

size_t printlnSim(const String &s)
{
return printSim(s + '\n');
}

class HardwareSerialSim
{
public:
void begin(unsigned long baudrate)
{
Serial.begin(baudrate);
}

size_t print(const String &s)
{
syncPinsWithSim();

DynamicJsonDocument jsonDoc(500);
jsonDoc["print"] = s;

return serializeJson(jsonDoc, Serial);
}

size_t print(int &s)
{
return print(String(s));
}

size_t println(const String &s)
{
return print(s + '\n');
}

size_t println(int &s)
{
return println(String(s));
}
};

HardwareSerialSim SerialSim;

#define Serial SerialSim
8 changes: 8 additions & 0 deletions Exercises/FiretruckSim/Boom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[<- Tilbake](/README.md#arbeidskrav)

# Firetruck Sim: Arm

Få armen til å bevege seg basert på kontrollpanelet.

- [Simulering](https://firetruck-sim.vercel.app/)
- [Kode](Boom.ino)
16 changes: 16 additions & 0 deletions Exercises/FiretruckSim/Boom/diagram.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": 1,
"author": "wokwi",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-uno",
"id": "uno",
"top": 0,
"left": 0,
"attrs": {}
}
],
"connections": [],
"dependencies": {}
}
1 change: 1 addition & 0 deletions Exercises/FiretruckSim/Boom/libraries.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ArduinoJson
6 changes: 6 additions & 0 deletions Exercises/FiretruckSim/Boom/wokwi.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[wokwi]
version = 1
firmware = "../../../.arduino/output/Boom.ino.hex"
elf = "../../../.arduino/output/Boom.ino.elf"
rfc2217ServerPort = 4_000
webSocketServerPort = 4_001
38 changes: 38 additions & 0 deletions Exercises/FiretruckSim/Complete/Complete.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "FiretruckSim.h"

const int HEADLIGHTS_PICKER_PIN = 7;
const int HEADLIGHTS_PIN = 8;

const int FLASH_LEFT_PICKER_PIN = 9;
const int FLASH_LEFT_PIN = 10;

const int FLASH_RIGHT_PICKER_PIN = 11;
const int FLASH_RIGHT_PIN = 12;

const int WIPER_PICKER_PIN = 13;
const int WIPER_PIN = 14;

const int BOOM_UP_PICKER_PIN = 15;
const int BOOM_DOWN_PICKER_PIN = 16;
const int BOOM_ELEVATION_PIN = 18;

const int BOOM_LEFT_PICKER_PIN = 19;
const int BOOM_RIGHT_PICKER_PIN = 20;
const int BOOM_ROTATION_PIN = 22;

const int BOOM_ELBOW_OUT_PICKER_PIN = 23;
const int BOOM_ELBOW_IN_PICKER_PIN = 24;
const int BOOM_ELBOW_PIN = 25;

const int BOOM_WRIST_PIN = 28;

void setup()
{
Serial.begin(9600);
}

void loop()
{
Serial.println("Hello World!");
delay(1000);
}
Loading

0 comments on commit 9edffc9

Please sign in to comment.