docs + first potmeter program

This commit is contained in:
2025-02-17 15:55:12 +01:00
parent b6a6f04333
commit 72bf01a813
37 changed files with 222 additions and 26 deletions

4
.gitignore vendored
View File

@@ -1,2 +1,4 @@
.vscode
*.png
*.png
*.JPEG
*.mov

Binary file not shown.

After

Width:  |  Height:  |  Size: 474 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 368 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 406 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 327 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 257 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 KiB

View File

@@ -153,10 +153,10 @@ In this case minVal is 800 an maxVal is 2200. So we need to stay between these r
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.png)
![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.png)
![alt text](image-4.jpg)
#### Setting the correct board
@@ -184,7 +184,7 @@ To get the correct configuration if it isn't listed on PlatformIO, is to go to t
#### 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.png)
![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`
@@ -194,23 +194,23 @@ When setting up PlatformIO on another system we ran into the issue that the mach
### 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.png)
![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.png)
![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.
![alt text](image-7.png)
![alt text](image-7.jpg)
From there I started out changing everything to BNO085 and also importing the correct library. As I have found the correct library earlier in the code.
![alt text](image-8.png)
![alt text](image-8.jpg)
### The purple text (Compiler flags)
The purple text are compiler flags. They tell the compiler what to do with certain pieces of code or variables.
@@ -227,42 +227,110 @@ The purple text are compiler flags. They tell the compiler what to do with certa
### Setting the correct library
I added a compiler case for my sensor and added the correct library to it.
![alt text](image-8.png)
![alt text](image-8.jpg)
### Setting correct value selection
I am not sure what it does. It sets some values for the calculations later on. So I copied over the configuration for the MPU6050 because it was the most similar to the BNO085. ![alt text](image-9.png)
I am not sure what it does. It sets some values for the calculations later on. So I copied over the configuration for the MPU6050 because it was the most similar to the BNO085. ![alt text](image-9.jpg)
### Setting up the sensor
For this part I went into the documentation of the Sparkfun BNO0xx library. I first looked through the rest of the code what values it wanted because with the BNO085 you need to enable which information you want.
![alt text](image-11.png)
![alt text](image-11.jpg)
When digging deeper into the driver to where it pulls the data from the sensors you can see what values it want's and according to that I can go through the documentation to enable the correct data outputs.
I went to the [examples list](https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library/tree/main/examples) of the library and clicked every data example I needed.
![alt text](image-12.png)
![alt text](image-12.jpg)
I first clicked the data type I needed.
![alt text](image-13.png)
![alt text](image-13.jpg)
Then I copied the line that enabled the data output.
![alt text](image-10.png)
![alt text](image-10.jpg)
Then I inserted it into the driver code.
### Getting data from the sensors
This step goes similar to the last step except you need to copy over another bit.
![alt text](image-14.png)
![alt text](image-14.jpg)
What this part does is acquire the sensor data and storing it in a variable. The same needs to be done in the driver like this.
![alt text](image-15.png)
![alt text](image-15.jpg)
The only issue is that the BNO085 can't output degrees. So we need to convert the radians to degrees using a simple formula I found on google.
![alt text](image-16.png)
![alt text](image-16.jpg)
The same also has to be done later on in the error correction algorithm
![alt text](image-17.png)
![alt text](image-17.jpg)
That was the last we needed to change. Hopefully it works now since I still need to fix some compile errors.
## Creating my own controls
I want to create my own controller. So I need to edit the current controls. The base driver attempts to set up radio communication. We won't be using that so I'm gonna comment that out.
![alt text](image-18.jpg)
When attempting to compile it still throws some errors so there are still functions being called from the original radio script. The error log indicated it comes from line 1239
![alt text](image-20.jpg)
![alt text](image-19.jpg)
So I will be needing to make my own case to control the drone.
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.
### Components for the controller
Im going to start off with 2 sliding potentiometers and a espC3 for throttle and for left-right. The Driver expects PWM frequencies so I'm sending PWM frequencies based on the potsliders.
![alt text](IMG_8180.jpg)
* [PotSlider](https://nl.aliexpress.com/item/1005006733220962.html) x2
* [espC3 supermini](https://nl.aliexpress.com/item/1005008125438785.html) x1
### 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.
![alt text](image-21.jpg)
Wiring it is pretty straight forward, VCC to 3v3, GND to GND and the out pin to a analog pin.
![alt text](IMG_8182.jpg)
### Programming the controller
#### Testing if I can upload
I started out by creating a new PlatformIO project. And selected the `Lolin Wemos C3 Mini`. Then I tried to upload some code to see if it worked.
![alt text](image-22.jpg)
In short the answer was yes. I was a bit surprised because the ESP-C3 supermini isn't in the list of platformIO.
#### Reading the Potentiometer
Reading the potentiometer is really straightforward. The only line we need is `analogRead()`, something to tell the pin that's it an input and something to print it to console.
```cpp
#include <Arduino.h>
void setup(){
Serial.begin(9600); //Opens serial bus
pinMode(0, INPUT); //sets pin 0 to input
}
void loop(){
Serial.println(analogRead(0)); //prints the result of analogRead() to console
}
```
When attempting to upload this I got this error a few times.
![alt text](image-23.jpg)
Then I remembered from the local lecture that if you `press and hold boot > press reset > release boot`. You can start the microcontroller in boot mode so you can easily flash to it.
After flashing it onto the microcontroller I could test it.
![alt text](image-24.jpg)
![alt text](image-25.jpg)
I've noticed there are some ghost values when it needs to be 0 so I need a small area where it's always 0. The upper side of the Potentiometer is always 4095 thats the max value so that's good.
##### Fixing the ghost measurements
Im going to map everything lower than 80 to 80 using software. The reason im doing 80 is because then it will keep on counting up from 80 instead of jumping from 0 to 81 and I didn't see any ghost readings above 80.

View File

@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

View File

@@ -0,0 +1,37 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the convention is to give header files names that end with `.h'.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

View File

@@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into the executable file.
The source code of each library should be placed in a separate directory
("lib/your_library_name/[Code]").
For example, see the structure of the following example libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
Example contents of `src/main.c` using Foo and Bar:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
The PlatformIO Library Dependency Finder will find automatically dependent
libraries by scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

View File

@@ -0,0 +1,14 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:lolin_c3_mini]
platform = espressif32
board = lolin_c3_mini
framework = arduino

View File

@@ -0,0 +1,13 @@
#include <Arduino.h>
void setup(){
Serial.begin(9600);
pinMode(0, INPUT);
}
void loop(){
Serial.println(analogRead(0));
}

View File

@@ -0,0 +1,11 @@
This directory is intended for PlatformIO Test Runner and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html

View File

@@ -220,12 +220,12 @@ const int ch5Pin = 21; // gear (throttle cut)
const int ch6Pin = 22; // aux1 (free aux channel)
const int PPM_Pin = 23;
// OneShot125 ESC pin outputs:
const int m1Pin = 0;
const int m2Pin = 1;
const int m3Pin = 2;
const int m4Pin = 3;
const int m5Pin = 4;
const int m6Pin = 5;
const int m1Pin = D0;
const int m2Pin = D1;
const int m3Pin = D2;
const int m4Pin = D3;
const int m5Pin = D4;
const int m6Pin = D5;
// PWM servo or ESC outputs:
const int servo1Pin = 6;
const int servo2Pin = 7;
@@ -371,7 +371,7 @@ void setup() {
delay(5);
// Initialize radio communication
radioSetup();
// radioSetup(); //commented out because I'm making my own controller
// Set radio channels to default (safe) values before entering main loop
channel_1_pwm = channel_1_fs;
@@ -602,7 +602,7 @@ void getIMUdata() {
#elif defined USE_MPU9250_SPI
mpu9250.getMotion9(&AcX, &AcY, &AcZ, &GyX, &GyY, &GyZ, &MgX, &MgY, &MgZ);
#elif defined USE_BNO085_I2C
//bno magnetometer
//BNO magnetometer
MgX = myIMU.getMagX();
MgY = myIMU.getMagY();
MgZ = myIMU.getMagZ();