mirror of
https://gitlab.waag.org/make/fablab/interns/2025/sam.git
synced 2025-08-04 04:14:56 +00:00
added sources
This commit is contained in:
@@ -364,9 +364,9 @@ During coding I realized this could be done way easier and simpler. So this is m
|
|||||||
```
|
```
|
||||||
This code works and makes any value under 80 automatically 80. And keeps all values above the original value.
|
This code works and makes any value under 80 automatically 80. And keeps all values above the original value.
|
||||||
|
|
||||||
I've cleaned it up a bit and turned it into a function so I don't have to copy paste code later on and so it stays clean.
|
I've cleaned it up a bit and turned it into a function so I don't have to copy paste code later on so it stays cleaner.
|
||||||
Result:
|
Result:
|
||||||
??? example
|
??? Code
|
||||||
```cpp
|
```cpp
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
// declarations
|
// declarations
|
||||||
@@ -384,10 +384,55 @@ Result:
|
|||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
|
||||||
Serial.println(normalizePot(potPin1, 80));
|
Serial.println(normalizePot(potPin1, 80)); //print the output of the function directly
|
||||||
}
|
}
|
||||||
|
|
||||||
int normalizePot(int pin, int minValue)
|
int normalizePot(int pin, int minValue) //function
|
||||||
|
{
|
||||||
|
int pot = analogRead(pin); //read the pin
|
||||||
|
|
||||||
|
if (pot <= minValue) //if the value is below 80 return 80 and go back to the loop
|
||||||
|
{
|
||||||
|
return 80;
|
||||||
|
}
|
||||||
|
else //else return the original value and return to the loop function
|
||||||
|
{
|
||||||
|
return pot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Now I wanna map the potmeter values as if it is a PWM signal. The PWM signal for this script goes from 1100 to 2200 as seen earlier in the documentation. I wanna be more on the safe side and I'm going for 1400 - 2000 range so I don't accidentally trigger the failsafe.
|
||||||
|
|
||||||
|
??? Code
|
||||||
|
```cpp
|
||||||
|
#include <Arduino.h>
|
||||||
|
// declarations
|
||||||
|
int normalizePot(int pin, int minValue);
|
||||||
|
int mapPot(int normalizedValue);
|
||||||
|
|
||||||
|
// constants
|
||||||
|
const int POTPIN1 = 0;
|
||||||
|
const int MAXPWMVALUE = 1400;
|
||||||
|
const int MINPWMVALUE = 2000;
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
Serial.begin(9600);
|
||||||
|
pinMode(POTPIN1, INPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
|
||||||
|
Serial.println(mapPot(normalizePot(POTPIN1, 80))); //call normalizePot and put the output into mapPot then print it
|
||||||
|
}
|
||||||
|
|
||||||
|
int mapPot(int normalizedValue){
|
||||||
|
return map(normalizedValue, 80, 4095, MINPWMVALUE, MAXPWMVALUE); //map the normalized value to the PWM range
|
||||||
|
}
|
||||||
|
|
||||||
|
int normalizePot(int pin, int minValue) //normalize the pot value to a range of 80 to 4095 instead of 0 to 4095 because the potmeter is at lower values not accurate
|
||||||
{
|
{
|
||||||
int pot = analogRead(pin);
|
int pot = analogRead(pin);
|
||||||
|
|
||||||
@@ -401,3 +446,25 @@ Result:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
This is my new code. It maps the values from 80 to 4095 to a range of 1400 to 2000.
|
||||||
|
|
||||||
|
#### Connection between the controller and the flight controller
|
||||||
|
Now that I have PWM values the driver can understand I wanna test if the driver works and if I can get readings from the driver. Because it needs some kind of controller script to start. For the wireless communication I wanna use ESPNOW, because the protocol works without internet. The 2 esp's directly connect to each other. The only downside is that you're stuck with esp's because the protocol only works on esp's.
|
||||||
|
|
||||||
|
I am following [this](https://randomnerdtutorials.com/esp-now-esp32-arduino-ide/) tutorial to create an ESPNOW connection.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Sources
|
||||||
|
### Code
|
||||||
|
* [espnow](https://randomnerdtutorials.com/esp-now-esp32-arduino-ide/)
|
||||||
|
* [arduino map function](https://docs.arduino.cc/language-reference/en/functions/math/map/)
|
||||||
|
* [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)
|
||||||
|
|
||||||
|
### Parts
|
||||||
|
* [PotSlider](https://nl.aliexpress.com/item/1005006733220962.html)
|
||||||
|
* [espC3 supermini](https://nl.aliexpress.com/item/1005008125438785.html)
|
||||||
|
Reference in New Issue
Block a user