Skip to content

Commit 67dd9d9

Browse files
authored
Merge pull request #3 from RoboticsBrno/mpu
Mpu + OLED
2 parents 07893c2 + f760939 commit 67dd9d9

12 files changed

Lines changed: 772 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@
3939

4040
# Doxygen export
4141
doc
42+
4243
.pio
4344
src/main.cpp

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
all:
22
pio ci examples/logger/main.cpp --lib src --project-conf platformio.ini
3-
pio ci examples/motors/main.cpp --lib src --project-conf platformio.ini
3+
pio ci examples/motors/main.cpp --lib src --project-conf platformio.ini
4+
5+
format:
6+
clang-format -i src/*.h src/*.cpp

examples/mpu/main.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "RBCX.h"
2+
#include <Arduino.h>
3+
#include <string>
4+
5+
void clear() {
6+
rb::Manager::get().oled().fill(rb::Oled::Black);
7+
rb::Manager::get().oled().updateScreen();
8+
}
9+
10+
void waitToNextTest() {
11+
delay(3000);
12+
clear();
13+
}
14+
15+
void setup() {
16+
printf("RB3204-RBCX\n");
17+
18+
delay(500);
19+
20+
printf("Init manager\n");
21+
auto& man = rb::Manager::get();
22+
man.install();
23+
24+
man.leds().red(true);
25+
26+
auto& mpu = rb::Manager::get().mpu();
27+
28+
mpu.init();
29+
mpu.sendStart();
30+
31+
while (true) {
32+
printf("MPU - angle: X: %2.2f Y: %2.2f Z: %2.2f\n", mpu.getAngleX(), mpu.getAngleY(), mpu.getAngleZ());
33+
delay(100);
34+
}
35+
36+
}
37+
38+
void loop() {}

examples/oled/main.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <Arduino.h>
2+
#include "RBCX.h"
3+
4+
void clear(){
5+
rb::Manager::get().oled().fill(rb::Oled::Black);
6+
rb::Manager::get().oled().updateScreen();
7+
}
8+
9+
void waitToNextTest() {
10+
delay(3000);
11+
clear();
12+
}
13+
14+
void setup() {
15+
printf("RB3204-RBCX\n");
16+
17+
delay(500);
18+
19+
printf("Init manager\n");
20+
auto& man = rb::Manager::get();
21+
man.install();
22+
23+
man.leds().red(true);
24+
25+
auto& oled = rb::Manager::get().oled();
26+
27+
oled.init(rb::Oled::Oled_128x64, true, false);
28+
// oled.init(rb::Oled::Oled_128x32, true, false);
29+
30+
while (true) {
31+
///////// fill ////////////////////////
32+
printf("fill\n");
33+
oled.fill(rb::Oled::White);
34+
oled.updateScreen();
35+
delay(1000);
36+
37+
oled.fill(rb::Oled::Black);
38+
oled.updateScreen();
39+
delay(1000);
40+
41+
42+
///////// drawPixel ////////////////////////
43+
printf("drawPixel\n");
44+
for(int i = 0; i<500; i++) {
45+
oled.drawPixel(random(0,oled.getWidth()), random(0, oled.getHeight()), rb::Oled::White);
46+
}
47+
oled.updateScreen();
48+
waitToNextTest();
49+
50+
51+
///////// setCursor ////////////////////////
52+
///////// writeString ////////////////////////
53+
printf("setCursor + writeString\n");
54+
oled.setCursor(45, 0);
55+
oled.writeString("OLED", rb::Oled::Font_11x18, rb::Oled::White);
56+
57+
oled.setCursor(5, oled.getHeight()/2);
58+
String text = "OLED w:" + String(oled.getWidth()) + " | h:" + String(oled.getHeight());
59+
oled.writeString(text, rb::Oled::Font_7x10, rb::Oled::White);
60+
oled.updateScreen();
61+
waitToNextTest();
62+
63+
64+
///////// drawLine ////////////////////////
65+
printf("drawLine\n");
66+
oled.drawLine(0, 0, oled.getWidth(), oled.getHeight(), rb::Oled::White);
67+
oled.drawLine(0, oled.getHeight(), oled.getWidth(), 0, rb::Oled::White);
68+
oled.updateScreen();
69+
waitToNextTest();
70+
71+
72+
///////// drawArc ////////////////////////
73+
printf("drawArc\n");
74+
oled.drawArc(oled.getWidth()/2, oled.getHeight()/2, 15, 20, 270, rb::Oled::White);
75+
oled.updateScreen();
76+
waitToNextTest();
77+
78+
79+
///////// drawCircle ////////////////////////
80+
printf("drawCircle\n");
81+
for(uint32_t delta = 0; delta < 5; delta ++) {
82+
oled.drawCircle(20* delta+30, 15, 10, rb::Oled::White);
83+
}
84+
oled.updateScreen();
85+
waitToNextTest();
86+
87+
///////// drawRectangle ////////////////////////
88+
printf("drawRectangle\n");
89+
for(int i = 5; i< oled.getWidth(); i+=5) {
90+
oled.drawRectangle(i, 0, i+i, oled.getHeight()-1, rb::Oled::White);
91+
}
92+
oled.updateScreen();
93+
waitToNextTest();
94+
95+
}
96+
}
97+
98+
void loop() {}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"version": "https://github.com/RoboticsBrno/Esp32-lx16a/archive/refs/tags/v1.2.1.zip"
3636
}
3737
],
38-
"version": "1.4.1",
38+
"version": "1.5.0",
3939
"frameworks": ["espidf", "arduino"],
4040
"platforms": "espressif32",
4141
"build": {

src/RBCXManager.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ void Manager::consumerRoutine() {
155155
case CoprocStat_stupidServoStat_tag:
156156
// Ignore
157157
break;
158+
case CoprocStat_mpuStat_tag:
159+
m_mpu.setState(msg.payload.mpuStat);
160+
break;
161+
case CoprocStat_faultStat_tag:
162+
fault(msg.payload.faultStat);
163+
break;
158164
default:
159165
printf("Received message of unknown type from stm32: %d\n",
160166
msg.which_payload);
@@ -261,6 +267,21 @@ bool Manager::printTasksDebugInfo() {
261267
}
262268
#endif
263269

270+
void Manager::fault(CoprocStat_FaultStat faultStat) {
271+
switch (faultStat.which_fault) {
272+
case CoprocStat_FaultStat_oledFault_tag:
273+
ESP_LOGE(TAG, "Oled not connected");
274+
break;
275+
276+
case CoprocStat_FaultStat_mpuFault_tag:
277+
ESP_LOGE(TAG, "MPU6050 not connected");
278+
break;
279+
280+
default:
281+
ESP_LOGE(TAG, "CoprocStat - non specific error");
282+
}
283+
}
284+
264285
MotorChangeBuilder::MotorChangeBuilder() {}
265286

266287
MotorChangeBuilder::MotorChangeBuilder(MotorChangeBuilder&& o)

src/RBCXManager.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#pragma once
22

3+
// clang-format off
34
#include <freertos/FreeRTOS.h>
45
#include <freertos/queue.h>
56
#include "freertos/semphr.h"
7+
// clang-format on
68
#include <functional>
79
#include <list>
810
#include <memory>
@@ -13,6 +15,8 @@
1315
#include "RBCXButtons.h"
1416
#include "RBCXLeds.h"
1517
#include "RBCXMotor.h"
18+
#include "RBCXMpu.h"
19+
#include "RBCXOled.h"
1620
#include "RBCXPiezo.h"
1721
#include "RBCXStupidServo.h"
1822
#include "RBCXTimers.h"
@@ -48,7 +52,7 @@ inline ManagerInstallFlags operator|(
4852
}
4953

5054
// Periodically print info about all RBCX tasks to the console
51-
//#define RB_DEBUG_MONITOR_TASKS 1
55+
// #define RB_DEBUG_MONITOR_TASKS 1
5256

5357
/**
5458
* \brief The main library class for working with the RBCX board.
@@ -99,6 +103,9 @@ class Manager {
99103

100104
StupidServo& stupidServo(uint8_t index);
101105

106+
Oled& oled() { return m_oled; } //!< Get the {@link Piezo} controller
107+
Mpu& mpu() { return m_mpu; } //!< Get the {@link Piezo} controller
108+
102109
// Pass to Esp32-lx16a library's begin()
103110
SmartServoBusBackend &smartServoBusBackend();
104111

@@ -159,6 +166,7 @@ class Manager {
159166
std::vector<TaskHandle_t> m_tasks;
160167
std::mutex m_tasks_mutex;
161168
#endif
169+
void fault(CoprocStat_FaultStat faultStat);
162170

163171
TaskHandle_t m_keepaliveTask;
164172

@@ -174,6 +182,8 @@ class Manager {
174182
TickType_t m_motors_last_set;
175183
Motor m_motors[size_t(MotorId::MAX)];
176184

185+
rb::Oled m_oled;
186+
rb::Mpu m_mpu;
177187
rb::Piezo m_piezo;
178188
rb::Leds m_leds;
179189
rb::Buttons m_buttons;

0 commit comments

Comments
 (0)