summaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp175
1 files changed, 129 insertions, 46 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 91b5394..2298f4d 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -5,6 +5,10 @@
#include <Adafruit_SSD1306.h>
#include <FluxGarage_RoboEyes.h>
+
+#include <WiFi.h>
+#include <WiFiUdp.h>
+
#include "settings.h"
#include "motors.h"
#include "rgb.h"
@@ -19,8 +23,10 @@ MOTORS motors;
TaskHandle_t TaskOled;
TaskHandle_t TaskRgb;
TaskHandle_t TaskMotors;
+TaskHandle_t TaskWiFi;
enum RobotEmotion {
+ MANUAL,
E_NEUTRAL,
E_HAPPY,
E_SAD,
@@ -29,17 +35,96 @@ enum RobotEmotion {
volatile RobotEmotion currentState = E_NEUTRAL;
+volatile char manualMotors = '0';
+
+String ipRobot = "Connection error";
+WiFiUDP udp;
+char incomingPacket[255];
+
+
+/*================================= WIFI =================================*/
+
+void wifiSetup(const char* password, const char* ssid) {
+ const int udpPort = 4210;
+
+ WiFi.mode(WIFI_STA);
+ WiFi.begin(ssid, password);
+
+ while (WiFi.status() != WL_CONNECTED) {
+ delay(500);
+ }
+
+ WiFi.setSleep(true);
+ udp.begin(udpPort);
+
+ ipRobot = WiFi.localIP().toString();
+}
+
+
+void wifiTask(void * pvParameters) {
+ unsigned long emotionStartTime = 0;
+ const unsigned long EMOTION_DURATION_MS = 2500;
+
+ for(;;) {
+ int packetSize = udp.parsePacket();
+
+ if (packetSize) {
+ int len = udp.read(incomingPacket, 255);
+ if (len > 0) {
+ incomingPacket[len] = 0;
+ char cmd = incomingPacket[0];
+
+ if (currentState == MANUAL) {
+ if (cmd == 'F') manualMotors = 'F';
+ else if (cmd == 'B') manualMotors = 'B';
+ else if (cmd == 'L') manualMotors = 'L';
+ else if (cmd == 'R') manualMotors = 'R';
+ else if (cmd == '0') manualMotors = '0';
+ }
+
+ if (cmd == 'H') { currentState = E_HAPPY; emotionStartTime = millis(); manualMotors = '0'; }
+ else if (cmd == 'S') { currentState = E_SAD; emotionStartTime = millis(); manualMotors = '0'; }
+ else if (cmd == 'A') { currentState = E_ANGRY; emotionStartTime = millis(); manualMotors = '0'; }
+ else if (cmd == 'N') { currentState = E_NEUTRAL; emotionStartTime = millis(); manualMotors = '0'; }
+ else if (cmd == 'M') { currentState = MANUAL; manualMotors = '0'; }
+ }
+ }
+
+ if (currentState != MANUAL) {
+ if (millis() - emotionStartTime >= EMOTION_DURATION_MS) {
+ currentState = MANUAL;
+ manualMotors = '0';
+ }
+ }
+
+ vTaskDelay(20 / portTICK_PERIOD_MS);
+ }
+}
+
+/*================================= OLED =================================*/
void oledSetup(){
Wire.begin(SDA_PIN, SCL_PIN);
delay(250);
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
roboEyes.begin(OLED_WIDTH, OLED_HEIGHT, 50);
+
+ display.clearDisplay();
+ display.setTextSize(1);
+ display.setTextColor(SSD1306_WHITE);
+ display.setCursor(0, 10);
+ display.println("Connected:");
+ display.print("IP: ");
+ display.println(ipRobot);
+ display.display();
+ delay(5000);
+
roboEyes.setAutoblinker(ON, 3, 2);
roboEyes.setIdleMode(ON, 2, 2);
}
void oledTask(void * pvParameters) {
+
RobotEmotion lastEmotion = E_NEUTRAL;
for(;;) {
@@ -61,13 +146,13 @@ void oledTask(void * pvParameters) {
break;
}
}
-
roboEyes.update();
-
vTaskDelay(20 / portTICK_PERIOD_MS);
}
}
+
+/*================================= RGB =================================*/
void rgbTask(void * pvParameters) {
RobotEmotion lastEmotion = E_NEUTRAL;
@@ -75,90 +160,88 @@ void rgbTask(void * pvParameters) {
for(;;) {
if (currentState != lastEmotion) {
-
switch(currentState) {
- case E_HAPPY:
- rgb.fadeColor(255, 250, 0, 500);
- break;
- case E_SAD:
- rgb.fadeColor(118, 116, 237, 500);
- break;
- case E_ANGRY:
- rgb.fadeColor(255, 0, 0, 500);
- break;
+ case E_HAPPY: rgb.fadeColor(255, 250, 0, 500); break;
+ case E_SAD: rgb.fadeColor(118, 116, 237, 500); break;
+ case E_ANGRY: rgb.fadeColor(255, 0, 0, 500); break;
case E_NEUTRAL:
- default:
- rgb.fadeColor(0, 0, 0, 500);
- break;
+ default: rgb.fadeColor(0, 0, 0, 500); break;
}
lastEmotion = currentState;
}
switch(currentState) {
case E_HAPPY:
- rgb.blink(1, 300);
+ rgb.setBrightness(40);
+ rgb.rainbowCycle(1000, 2);
break;
case E_SAD:
- rgb.breathe(1, 40);
+ rgb.setBrightness(40);
break;
case E_ANGRY:
+ rgb.setBrightness(100);
rgb.blink(1, 300);
break;
case E_NEUTRAL:
default:
break;
}
-
vTaskDelay(20 / portTICK_PERIOD_MS);
}
}
+
+/*================================= MOTORS =================================*/
+
void motorsTask(void * pvParameters) {
for(;;) {
- switch(currentState) {
- case E_HAPPY:
- motors.leftTurn(100, 1000);
- motors.rightTurn(100, 1000);
- break;
- case E_SAD:
- motors.stop();
- break;
- case E_ANGRY:
- motors.forward(100, 1000);
- motors.backward(100, 1000);
- break;
- default:
- motors.stop();
- break;
+ if (currentState == MANUAL) {
+ switch(manualMotors) {
+ case 'F': motors.forward(100, 50); break;
+ case 'B': motors.backward(100, 50); break;
+ case 'L': motors.leftTurn(100, 50); break;
+ case 'R': motors.rightTurn(100, 50); break;
+ default: motors.stop(); break;
+ }
+ } else {
+ switch(currentState) {
+ case E_HAPPY:
+ motors.leftTurn(100, 1000);
+ motors.rightTurn(100, 1000);
+ break;
+ case E_ANGRY:
+ motors.forward(100, 1000);
+ motors.backward(100, 1000);
+ break;
+ default:
+ motors.stop();
+ break;
+ }
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
+
+/*================================= SETUP =================================*/
+
void setup() {
+ wifiSetup("haslo12345", "siemamaciek");
rgb.init();
motors.init();
oledSetup();
-
+
xTaskCreate(oledTask, "OLED Task", 4096, NULL, 1, &TaskOled);
xTaskCreate(rgbTask, "RGB Task", 2048, NULL, 1, &TaskRgb);
xTaskCreate(motorsTask, "Motors Task", 2048, NULL, 1, &TaskMotors);
+ xTaskCreate(wifiTask, "WiFi Task", 4096, NULL, 1, &TaskWiFi);
- currentState = E_NEUTRAL;
+ currentState = MANUAL;
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
void loop() {
- currentState = E_HAPPY;
- vTaskDelay(5000 / portTICK_PERIOD_MS);
-
- currentState = E_NEUTRAL;
- vTaskDelay(5000 / portTICK_PERIOD_MS);
-
- currentState = E_ANGRY;
- vTaskDelay(5000 / portTICK_PERIOD_MS);
-
- currentState = E_SAD;
- vTaskDelay(5000 / portTICK_PERIOD_MS);
+ vTaskDelay(1000 / portTICK_PERIOD_MS);
}
+