1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <FluxGarage_RoboEyes.h>
#include "settings.h"
#include "motors.h"
#include "rgb.h"
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);
RoboEyes<Adafruit_SSD1306> roboEyes(display);
RGB rgb;
MOTORS motors;
TaskHandle_t TaskOled;
TaskHandle_t TaskRgb;
TaskHandle_t TaskMotors;
enum RobotEmotion {
E_NEUTRAL,
E_HAPPY,
E_SAD,
E_ANGRY
};
volatile RobotEmotion currentState = E_NEUTRAL;
void oledSetup(){
Wire.begin(SDA_PIN, SCL_PIN);
delay(250);
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
roboEyes.begin(OLED_WIDTH, OLED_HEIGHT, 50);
roboEyes.setAutoblinker(ON, 3, 2);
roboEyes.setIdleMode(ON, 2, 2);
}
void oledTask(void * pvParameters) {
RobotEmotion lastEmotion = E_NEUTRAL;
for(;;) {
if (currentState != lastEmotion) {
lastEmotion = currentState;
switch(currentState) {
case E_HAPPY:
roboEyes.setMood(HAPPY);
break;
case E_SAD:
roboEyes.setMood(TIRED);
break;
case E_ANGRY:
roboEyes.setMood(ANGRY);
break;
default:
roboEyes.setMood(DEFAULT);
break;
}
}
roboEyes.update();
vTaskDelay(20 / portTICK_PERIOD_MS);
}
}
void rgbTask(void * pvParameters) {
RobotEmotion lastEmotion = E_NEUTRAL;
rgb.setColorRGB(0, 0, 0, true);
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_NEUTRAL:
default:
rgb.fadeColor(0, 0, 0, 500);
break;
}
lastEmotion = currentState;
}
switch(currentState) {
case E_HAPPY:
rgb.blink(1, 300);
break;
case E_SAD:
rgb.breathe(1, 40);
break;
case E_ANGRY:
rgb.blink(1, 300);
break;
case E_NEUTRAL:
default:
break;
}
vTaskDelay(20 / portTICK_PERIOD_MS);
}
}
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;
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void setup() {
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);
currentState = E_NEUTRAL;
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);
}
|