Compare commits

...

10 Commits

Author SHA1 Message Date
84fffaf40d notes about platformio 2025-02-19 13:49:11 +01:00
d7aecf60ea Added new MCU 2025-02-19 13:20:58 +01:00
a1203b6b9d easier readibility 2025-02-19 13:19:38 +01:00
c52161f3b4 change docs order 2025-02-19 13:08:29 +01:00
2b16ea154f more typos 2025-02-19 13:00:30 +01:00
83e63ccba5 debug level docs 2025-02-19 11:14:54 +01:00
84fc9a93c1 fix video 2025-02-19 11:01:56 +01:00
e9231e92aa fix typo 2025-02-19 11:00:53 +01:00
bbfd4965d8 fix video 2025-02-19 10:59:21 +01:00
df4145c734 docs - it finally compiles now except the IMU is dead 2025-02-19 10:56:04 +01:00
19 changed files with 565 additions and 369 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 389 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

View File

@@ -3,6 +3,63 @@
## Introduction
I'm very familiar with coding in C++ because of my university. At the moment im studying computer sciences at the Applied University of Amsterdam.
### Switching to platformIO.
At the local lecture we got thought how to use platformIO. I was already used to Arduino IDE v2. But I found the UI and the speeds of PlatformIO very nice so that's why I decided to switch to PlatformIO.
First off. I created a new project with the Xiao espC3 because the espC6 isn't registered on the list. I've googled a bit and that's because PlatformIO needs to get profit so they ask money to the board developers to add the board to their platform. So we need to set the correct board later on.
![alt text](image-3.jpg)
When it created the project. I copied all the files over to the src folder and renament my drone.ino to main.cpp. Now I had a lot of include errors because Arduino IDE automatically imports everything and with PlatformIO you need to define everything yourself. So I started out in the Library manager installing all the libraries to the project I needed.
![alt text](image-4.jpg)
#### Setting the correct board
Before
```ini
[env:seeed_xiao_esp32c3]
platform = espressif32
board = seeed_xiao_esp32c3
framework = arduino
```
After
```ini
[env:seeed_xiao_esp32c6]
platform = https://github.com/mnowak32/platform-espressif32.git#boards/seeed_xiao_esp32c6
platform_packages =
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.2
framework-arduinoespressif32-libs @ https://github.com/espressif/arduino-esp32/releases/download/3.0.2/esp32-arduino-libs-3.0.2.zip
framework = arduino
board = seeed_xiao_esp32c6
```
To get the correct configuration if it isn't listed on PlatformIO, is to go to the Board manufactors website and hope there is a PlatformIO example configuration. For example this example was listed on the Xiao website. [Link](https://wiki.seeedstudio.com/xiao_esp32c6_with_platform_io/).
#### Installing libraries in platformIO
To install a library click the platformIO icon. In there click the libraries button. Then you can search a library and click add to project. Now you have added a library to your project!
![alt text](image.jpg)
After that it will edit platformio.ini and there you will see the library added to the project. It's also in `.pio/libdeps`
### Important for linux users.
When setting up PlatformIO on another system we ran into the issue that the machine couldn't upload the code properly. We first checked how the OS interacted with `dmesg`. `dmesg` on linux shows the kernel activity. After a while we found out we needed to install the package `platformio-udev`. What it does it add some rules for the kernel on how to handle the mcu's.
### Programming and compiling and uploading
Using platformIO is really straight forward. The only folder you should look at in your platformIO project is the `src` folder.
![alt text](image-5.jpg)
In the main.cpp file you can write your program to run on a microcontroller.
On the bottom of your screen you see that there are a few new buttons.
![alt text](image-6.jpg)
The Compile and Upload and Serial console buttons are going to be the most used. Compile compiles the code and checks if it's correct. If it has incorrect syntax you need to correct it. The upload button compiles the code first and then sends it to the Microcontroller. The Serial console is a really nice debug tool to see what the microcontroller is doing if you added print statements in your code.
## First code to read from the BNO085
I first wanted to create the drone firmware myself. But then I looked through some research papers and existing programs and saw all the math that was needed to keep it upright. Then I decided to modify an existing program.
This was my first attempts at getting the sensor to read
@@ -117,93 +174,6 @@ First used the wrong library I used the Adafruit bno0xx library instead of the S
## New driver
After researching for a while and looking through other fab academy projects I found out that other people also made drones with micro controllers and used a pre-made driver that they customized (https://fab.cba.mit.edu/classes/863.23/Architecture/people/Zhixing/finalproject.html). After doing some research on how to keep the drone upright I also decided to use an existing driver because the math required for that is way above my level. Im gonna be using the [dRhemFlightVTOL](https://github.com/nickrehm/dRehmFlight/tree/master) driver. The only problem is that it doesn't support my Inertial measuring unit (BNO085). So I will have to customize the driver to make it work with it.
### Implementing the BNO085
### Hijacking the controls
I need to reverse engineer the controls because now it just calls a function called radiocontrols() that manages the controls. There are 4 pwm channel that controls where the drone is going. I wanna make my own controller and simulate these pwm signals.
```cpp
thro_des = (channel_1_pwm - 1000.0) / 1000.0; // Between 0 and 1
roll_des = (channel_2_pwm - 1500.0) / 500.0; // Between -1 and 1
pitch_des = (channel_3_pwm - 1500.0) / 500.0; // Between -1 and 1
yaw_des = (channel_4_pwm - 1500.0) / 500.0; // Between -1 and 1
```
From looking further in the code it has safety protocols when the channels go too low or too high so using that we have a range where the pwm speed should be.
```cpp
// Triggers for failure criteria
// Line 1260
if (channel_1_pwm > maxVal || channel_1_pwm < minVal)
check1 = 1;
if (channel_2_pwm > maxVal || channel_2_pwm < minVal)
check2 = 1;
if (channel_3_pwm > maxVal || channel_3_pwm < minVal)
check3 = 1;
if (channel_4_pwm > maxVal || channel_4_pwm < minVal)
check4 = 1;
if (channel_5_pwm > maxVal || channel_5_pwm < minVal)
check5 = 1;
if (channel_6_pwm > maxVal || channel_6_pwm < minVal)
check6 = 1;
```
In this case minVal is 800 an maxVal is 2200. So we need to stay between these ranges to control the drone. In theory you can send any value to the ESC because it calibrates to the lowest and highest values you give it but there is a range. From past experiences I have experienced that 12bit PWM is not sufficient, 16 bit is needed.
[ESC calibration](https://ardupilot.org/copter/docs/esc-calibration.html)
### Switching to platformIO.
At the local lecture we got thought how to use platformIO. I was already used to Arduino IDE v2. But I found the UI and the speeds of PlatformIO very nice so that's why I decided to switch to PlatformIO.
First off. I created a new project with the Xiao espC3 because the espC6 isn't registered on the list. I've googled a bit and that's because PlatformIO needs to get profit so they ask money to the board developers to add the board to their platform. So we need to set the correct board later on.
![alt text](image-3.jpg)
When it created the project. I copied all the files over to the src folder and renament my drone.ino to main.cpp. Now I had a lot of include errors because Arduino IDE automatically imports everything and with PlatformIO you need to define everything yourself. So I started out in the Library manager installing all the libraries to the project I needed.
![alt text](image-4.jpg)
#### Setting the correct board
Before
```ini
[env:seeed_xiao_esp32c3]
platform = espressif32
board = seeed_xiao_esp32c3
framework = arduino
```
After
```ini
[env:seeed_xiao_esp32c6]
platform = https://github.com/mnowak32/platform-espressif32.git#boards/seeed_xiao_esp32c6
platform_packages =
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.2
framework-arduinoespressif32-libs @ https://github.com/espressif/arduino-esp32/releases/download/3.0.2/esp32-arduino-libs-3.0.2.zip
framework = arduino
board = seeed_xiao_esp32c6
```
To get the correct configuration if it isn't listed on PlatformIO, is to go to the Board manufactors website and hope there is a PlatformIO example configuration. For example this example was listed on the Xiao website. [Link](https://wiki.seeedstudio.com/xiao_esp32c6_with_platform_io/).
#### Installing libraries in platformIO
To install a library click the platformIO icon. In there click the libraries button. Then you can search a library and click add to project. Now you have added a library to your project!
![alt text](image.jpg)
After that it will edit platformio.ini and there you will see the library added to the project. It's also in `.pio/libdeps`
### Important for linux users.
When setting up PlatformIO on another system we ran into the issue that the machine couldn't upload the code properly. We first checked how the OS interacted with `dmesg`. `dmesg` on linux shows the kernel activity.
### Programming and compiling and uploading
Using platformIO is really straight forward. The only folder you should look at in your platformIO project is the `src` folder.
![alt text](image-5.jpg)
In the main.cpp file you can write your program to run on a microcontroller.
On the bottom of your screen you see that there are a few new buttons.
![alt text](image-6.jpg)
The Compile and Upload and Serial console buttons are going to be the most used. Compile compiles the code and checks if it's correct. If it has incorrect syntax you need to correct it. The upload button compiles the code first and then sends it to the Microcontroller. The Serial console is a really nice debug tool to see what the microcontroller is doing if you added print statements in your code.
## Editing the new driver
Adding in support for the BNO085. The original driver doesn't support the BNO085.
I first started off searching for every instance of an existing sensor.
@@ -222,7 +192,7 @@ The purple text are compiler flags. They tell the compiler what to do with certa
| #elif | Same as #if except using this you can have multiple cases |
| #include | Include a library into the code |
| #else | If none of the cases are met this is used |
| #error | Throws an error during compiling when it is compiled |
| #error | Throws an error during compiling when ran by the compiler |
| #endif | end of a #if, #elif or #else |
### Setting the correct library
@@ -277,6 +247,38 @@ When attempting to compile it still throws some errors so there are still functi
![alt text](image-19.jpg)
So I will be needing to make my own case to control the drone.
### Hijacking the controls from the original driver.
I need to reverse engineer the controls because now it just calls a function called radiocontrols() that manages the controls. There are 4 pwm channel that controls where the drone is going. I wanna make my own controller and simulate these pwm signals.
```cpp
thro_des = (channel_1_pwm - 1000.0) / 1000.0; // Between 0 and 1
roll_des = (channel_2_pwm - 1500.0) / 500.0; // Between -1 and 1
pitch_des = (channel_3_pwm - 1500.0) / 500.0; // Between -1 and 1
yaw_des = (channel_4_pwm - 1500.0) / 500.0; // Between -1 and 1
```
From looking further in the code it has safety protocols when the channels go too low or too high so using that we have a range where the pwm speed should be.
```cpp
// Triggers for failure criteria
// Line 1260
if (channel_1_pwm > maxVal || channel_1_pwm < minVal)
check1 = 1;
if (channel_2_pwm > maxVal || channel_2_pwm < minVal)
check2 = 1;
if (channel_3_pwm > maxVal || channel_3_pwm < minVal)
check3 = 1;
if (channel_4_pwm > maxVal || channel_4_pwm < minVal)
check4 = 1;
if (channel_5_pwm > maxVal || channel_5_pwm < minVal)
check5 = 1;
if (channel_6_pwm > maxVal || channel_6_pwm < minVal)
check6 = 1;
```
In this case minVal is 800 an maxVal is 2200. So we need to stay between these ranges to control the drone. In theory you can send any value to the ESC because it calibrates to the lowest and highest values you give it but there is a range. From past experiences I have experienced that 12bit PWM is not sufficient, 16 bit is needed.
[ESC calibration](https://ardupilot.org/copter/docs/esc-calibration.html)
I wanna control to drone using another microcontroller, specifically an esp because then I can use the espNOW protocol to connect them together and get communication running between them. First I wanna make something functional before I upgrade it so that's why I'm starting out easy.
@@ -286,11 +288,11 @@ Im going to start off with 2 sliding potentiometers and a espC3 for throttle and
![alt text](IMG_8180.jpg)
* [PotSlider](https://nl.aliexpress.com/item/1005006733220962.html) x2
* [espC3 supermini](https://nl.aliexpress.com/item/1005008125438785.html) x1
* ~~[espC3 supermini](https://nl.aliexpress.com/item/1005008125438785.html) x1~~
* [MCU changed to espC3 XIAO](https://www.seeedstudio.com/Seeed-XIAO-ESP32C3-p-5431.html)
### Wiring the controller
To read the Pot meter values we need to read the resistance from the potslider. So we need analog read. When looking at the datasheet I can see that I can use pins from 0 to 5.
To read the Pot meter values we need to read the resistance from the potslider. So we need analog read. When looking at the data sheet I can see that I can use pins from 0 to 5.
![alt text](image-21.jpg)
@@ -457,7 +459,7 @@ I am following [this](https://randomnerdtutorials.com/esp-now-esp32-arduino-ide/
What I first need to do according to the tutorial is get the receiver esp their mac adress. Luckily that gets displayed when uploading to the esp.
![alt text](image-26.png)
![alt text](image-26.jpg)
After `MAC` there is the mac address. I've saved it in my controller code as a compiler flag `#define receiverMAC "d8:3b:da:37:66:00";` So I could use it later on. I don't know if the libraries accepts this compiler flag but I will find out soon enough.
I first need to create a struct with the data I wanna send. A stuct is a collection of variables named under one big variable.
@@ -573,7 +575,7 @@ I added that it could connect to the internet and that it attempts to send data.
Now when I compile it I get errors that the data isn't being send correctly. The issue could be that the second microcontroller isn't turned on with the receiver code.
![alt text](image-27.png)
![alt text](image-27.jpg)
#### Drone side
@@ -647,16 +649,16 @@ For the drone I'm going to start of with a seperate script to see if it works an
A weird thing I found out is that my esp32C3 Supermini doesn't work wirelessly that well. When I uploaded the code it still didn't work untill I held my finger on the antenna.
<video controls src="IMG_8197(1).mp4" title="Title" type="video/mp4"></video>
<video controls src="../../../Assignments/week_4_programming/IMG_8197(1).mp4" title="Title" type="video/mp4"></video>
I've delved a bit deeper into it and then I found online that with the first batches of espC3 supermini's have issues with the antenna. When I was debugging it I noticed that connecting the ground to the antenne fixed it when I tried it with a pin. But when soldered on it didn't work anymore and then only thing that worked was my finger.
I've delved a bit deeper into it and then I found online that with the first batches of espC3 supermini's have issues with the antenna. When I was debugging it I noticed that connecting the ground to the antenna fixed it when I tried it with a pin. But when soldered on it didn't work anymore and then only thing that worked was my finger.
![alt text](IMG_8200.JPEG)
![alt text](IMG_8200.jpg)
Result of bridging ground and the antenna. Sadly didn't work. After that I grabbed the Xiao espC3 and connected it. It almost worked like a drop in replacement I only needed to change the Potpin. After that it worked flawlessly.
![alt text](image-28.png)
![alt text](image-28.jpg)
Now I can start intergrating the code into the drone driver.
Now I can start integrating the code into the drone driver.
The first thing I did is add the correct libraries at the top of the file. So it looks like this now
@@ -670,25 +672,57 @@ The first thing I did is add the correct libraries at the top of the file. So it
After that I added my own ESPNow define so I could use that later on in the code. I want the code to stay variable and user specified so thats why im continuing the way it was made.
![alt text](image-29.png)
![alt text](image-29.jpg)
After that I added the global variables for the ESPNow protocol.
![alt text](image-30.png)
![alt text](image-30.jpg)
Then I uncommented `RadioSetup()` again so it could be used again and I added a case for my ESPNow protocol.
![alt text](image-31.png)
![alt text](image-31.jpg)
In the PWM receive function I added an additional case so my PWM values actually get intergrated into the code.
![alt text](image-32.png)
![alt text](image-32.jpg)
At the end of the program I added the ESPNow Initialisation function and the callback function for when the data is received.
At the end of the program I added the ESPNow Initialization function and the callback function for when the data is received.
![alt text](image-33.png)
![alt text](image-33.jpg)
When I comppile it now it should work.
When I compile it now it should work. Except that it doesn't work.
![alt text](image-34.jpg)
From looking at the errors it looked like the PWMServo library wasn't compatible with the ESP32. When looking around on google from PWMservo libraries for the ESP32 I found the correct library on [this](https://randomnerdtutorials.com/esp32-servo-motor-web-server-arduino-ide/) website. When I added that I needed to edit the class creation to the correct library and then it finally compiles!
From:
![alt text](image-35.jpg)
To:
![alt text](image-36.jpg)
Result:
![alt text](image-37.jpg)
It compiles!
The next day when testing the code. The microcontroller would suddenly disconnect. First I tried setting every pin to something else because there where pins being defined the espC6 doesn't have. When I finally set the debug level to verbose.
I did that by adding this line in my platformio.ini file. [Source](https://community.platformio.org/t/how-to-set-up-log-level-to-be-able-to-debug-the-esp32/8278)
```ini
build_flags = -DCORE_DEBUG_LEVEL=5
```
Now I can see everything that the microcontroller is doing. I saw that it used pin 12 and 13 for USB. And somewhere in the program pin 12 and 13 where re-assigned. So I removed that it got reassigned and then the communication worked flawlessly again.
![alt text](image-38.jpg)
Now I can finally test. When adding debug print statements for my sensor I found out it wasn't responding. Then I looked at the pins of the sensor and found out when I re-seated it I accidentally killed it.
I had it like this:
![alt text](IMG_8212.jpg)
Instead of like this.
![alt text](IMG_8213.jpg)
So I had the 3v3 connected to the sensor it's ground and the ground connected to the SCL pin. So that killed it.
## Sources
### Code
@@ -697,6 +731,8 @@ When I comppile it now it should work.
* [BNO085 library examples](https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library/tree/main/examples)
* [PlatformIO espC6](https://wiki.seeedstudio.com/xiao_esp32c6_with_platform_io)
* [ESC calibration and PWM](https://ardupilot.org/copter/docs/esc-calibration.html)
* [ESP32PWMServo lib](https://randomnerdtutorials.com/esp32-servo-motor-web-server-arduino-ide/)
* [PlatformIO Debug level change for ESP](https://community.platformio.org/t/how-to-set-up-log-level-to-be-able-to-debug-the-esp32/8278)
### Parts
* [PotSlider](https://nl.aliexpress.com/item/1005006733220962.html)

View File

@@ -11,10 +11,12 @@
[env:seeed_xiao_esp32c6]
platform = https://github.com/mnowak32/platform-espressif32.git#boards/seeed_xiao_esp32c6
platform_packages =
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.2
framework-arduinoespressif32-libs @ https://github.com/espressif/arduino-esp32/releases/download/3.0.2/esp32-arduino-libs-3.0.2.zip
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.2
framework-arduinoespressif32-libs @ https://github.com/espressif/arduino-esp32/releases/download/3.0.2/esp32-arduino-libs-3.0.2.zip
framework = arduino
board = seeed_xiao_esp32c6
lib_deps =
paulstoffregen/PWMServo@^2.1
sparkfun/SparkFun BNO080 Cortex Based IMU@^1.1.12
madhephaestus/ESP32Servo@^3.0.6
build_flags = -DCORE_DEBUG_LEVEL=5

File diff suppressed because it is too large Load Diff