Skip to content
Snippets Groups Projects
Commit a2feb1c3 authored by David Huss's avatar David Huss :speech_balloon:
Browse files

Added ESP32Servo.h + Ticker

parent 1d1e3f52
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:c44042ce-0838-4bde-a521-72c7e7d196d6 tags:
``` python
import csv
data = []
with open('data/open-meteo-53.53N9.98E11m (3) (2).csv') as csvfile:
reader = csv.DictReader(csvfile, delimiter=',')
for row in reader:
datum = row['rain_sum']
data.append(float(datum))
```
%% Cell type:code id:c70207fe-f427-4e92-813c-698cf94855b6 tags:
``` python
maximum = max(data)
normalized_data = [d/maximum for d in data]
with open("data.h", "w") as file:
file.write("#pragma once\n\n")
file.write(f"#define DATA_LENGTH {len(normalized_data)}\n\n")
file.write(f"float data[{len(normalized_data)}] = {{\n")
for d in normalized_data:
file.write(f" {d},\n")
file.write("};\n")
```
%% Cell type:code id:1fa46b19-0698-47bb-9f6d-e45ab7f2c96f tags:
``` python
from datetime import timedelta, datetime
t = 9.48 * 3
duration = timedelta(seconds=(len(normalized_data)*t))
print(duration)
```
%% Output
3 days, 0:00:02.160000
%% Cell type:code id:20be4f8c-56b4-45c4-a7f5-8885a1345ea1 tags:
``` python
t
```
%% Output
28.44
%% Cell type:code id:35be51de-976f-4f2a-a707-5dca0e213e18 tags:
``` python
```
#include "data.h"
#define SERVO_MIN 30
#define SERVO_MAX 480
int servoPin = 23;
int pos = SERVO_MIN;
int data_index = 0;
void setServoNormalized(float pos) {
pos = min(1.0f, max(0.0f, pos));
int span = SERVO_MAX - SERVO_MIN;
int servo_pos = SERVO_MIN + span * pos;
analogWrite(servoPin, servo_pos);
}
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
pinMode(servoPin, OUTPUT);
analogWriteFrequency(servoPin, 50);
analogWriteResolution(servoPin, 12);
}
void loop() {
// put your main code here, to run repeatedly:
float datum = data[data_index];
setServoNormalized(datum);
Serial.print("Aktueller Wert ");
Serial.print(datum * 67.2);
Serial.print(" mm --> ");
Serial.println(datum);
data_index++;
if ( data_index >= DATA_LENGTH) {
data_index = 0;
Serial.println("Die daten sind durch, wir fangen wieder vorne an...");
}
delay(28.44*1000);
}
{"data":{"layout-restorer:data":{"main":{"dock":{"type":"tab-area","currentIndex":0,"widgets":[]}},"down":{"size":0,"widgets":[]},"left":{"collapsed":false,"visible":true,"current":"filebrowser","widgets":["filebrowser","running-sessions","@jupyterlab/toc:plugin","extensionmanager.main-view"],"widgetStates":{"jp-running-sessions":{"sizes":[0.16666666666666666,0.16666666666666666,0.16666666666666666,0.16666666666666666,0.16666666666666666,0.16666666666666666],"expansionStates":[false,false,false,false,false,false]},"extensionmanager.main-view":{"sizes":[0.3333333333333333,0.3333333333333333,0.3333333333333333],"expansionStates":[false,false,false]}}},"right":{"collapsed":true,"visible":true,"widgets":["jp-property-inspector","debugger-sidebar"],"widgetStates":{"jp-debugger-sidebar":{"sizes":[0.2,0.2,0.2,0.2,0.2],"expansionStates":[false,false,false,false,false]}}},"relativeSizes":[0.26227795193312436,0.7377220480668757,0],"top":{"simpleVisibility":true}},"docmanager:recents":{"opened":[{"path":"","contentType":"directory","root":"~/Arduino/precipitationctl"},{"path":"Untitled.ipynb","contentType":"notebook","factory":"Notebook","root":"~/Arduino/precipitationctl"}],"closed":[{"path":"Untitled.ipynb","contentType":"notebook","factory":"Notebook","root":"~/Arduino/precipitationctl"}]}},"metadata":{"id":"default"}}
\ No newline at end of file
{"data":{"layout-restorer:data":{"main":{"dock":{"type":"tab-area","currentIndex":1,"widgets":["notebook:data-processing.ipynb"]},"current":"notebook:data-processing.ipynb"},"down":{"size":0,"widgets":[]},"left":{"collapsed":false,"visible":true,"current":"filebrowser","widgets":["filebrowser","running-sessions","@jupyterlab/toc:plugin","extensionmanager.main-view"],"widgetStates":{"jp-running-sessions":{"sizes":[0.16666666666666666,0.16666666666666666,0.16666666666666666,0.16666666666666666,0.16666666666666666,0.16666666666666666],"expansionStates":[false,false,false,false,false,false]},"extensionmanager.main-view":{"sizes":[0.3333333333333333,0.3333333333333333,0.3333333333333333],"expansionStates":[false,false,false]}}},"right":{"collapsed":true,"visible":true,"widgets":["jp-property-inspector","debugger-sidebar"],"widgetStates":{"jp-debugger-sidebar":{"sizes":[0.2,0.2,0.2,0.2,0.2],"expansionStates":[false,false,false,false,false]}}},"relativeSizes":[0.26227795193312436,0.7377220480668757,0],"top":{"simpleVisibility":true}},"docmanager:recents":{"opened":[{"path":"","contentType":"directory","root":"~/Arduino/precipitationctl"},{"path":"data-processing.ipynb","contentType":"notebook","factory":"Notebook","root":"~/Arduino/precipitationctl"},{"path":"precipitationctl.ino","contentType":"file","factory":"Editor","root":"~/Arduino/precipitationctl"}],"closed":[{"path":"precipitationctl.ino","contentType":"file","factory":"Editor","root":"~/Arduino/precipitationctl"}]},"notebook:data-processing.ipynb":{"data":{"path":"data-processing.ipynb","factory":"Notebook"}}},"metadata":{"id":"default"}}
\ No newline at end of file
This diff is collapsed.
#include "data.h"
#include <Ticker.h>
#include <ESP32Servo.h>
#define SERVO_MIN 0
#define SERVO_MAX 180
#define ADC_MAX 4096
#define INTERVAL 5
// 28.44
#define SERVO_MIN 30
#define SERVO_MAX 480
int servoPin = 23;
int pos = SERVO_MIN;
Servo servo;
int data_index = 0;
int data_index = 23;
double ptarget = 0.0f;
double target = 0.0f;
double current = 0.0f;
double blend = 0.0f;
void setServoNormalized(float pos) {
pos = min(1.0f, max(0.0f, pos));
int span = SERVO_MAX - SERVO_MIN;
int servo_pos = SERVO_MIN + span * pos;
analogWrite(servoPin, servo_pos);
Ticker ticker;
double lerp(double a, double b, double t) {
t = max(0.0, min(1.0, t));
return a + t * (b - a);
}
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
pinMode(servoPin, OUTPUT);
analogWriteFrequency(servoPin, 50);
analogWriteResolution(servoPin, 12);
void setServoNormalized(double pos) {
pos = min(1.0, max(0.0, pos));
//
pos = sqrt(pos);
int span = abs(SERVO_MAX - SERVO_MIN);
int servo_pos = SERVO_MIN + span * pos;
// Serial.print(" --> ");
// Serial.print(pos);
// Serial.print(" --> ");
// Serial.println(servo_pos);
Serial.print(SERVO_MIN); // To freeze the lower limit
Serial.print(" ");
Serial.print(SERVO_MAX); // To freeze the upper limit
Serial.print(" ");
Serial.println(servo_pos);
servo.write(servo_pos);
}
void loop() {
// put your main code here, to run repeatedly:
float datum = data[data_index];
setServoNormalized(datum);
Serial.print("Aktueller Wert ");
Serial.print(datum * 67.2);
Serial.print(" mm --> ");
Serial.println(datum);
void setNewTarget() {
ptarget = target;
target = data[data_index];
current = target;
// Serial.print("New Target is [");
// Serial.print(data_index);
// Serial.print("]: ");
// Serial.print(target);
data_index++;
// Reset index when longer than the data (loop over)
if (data_index >= DATA_LENGTH) {
data_index = 0;
Serial.println("Die daten sind durch, wir fangen wieder vorne an...");
}
delay(28.44*1000);
}
float testval = 0.0f;
float increment = 0.01f;
void setup() {
Serial.begin(115200);
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
servo.setPeriodHertz(50);// Standard 50hz servo
servo.attach(servoPin, 1000, 2000); // attaches the servo on pin 18 to the servo object
// using SG90 servo min/max of 500us and 2400us
// for MG995 large servo, use 1000us and 2000us,
// which are the defaults, so this line could be
// "myservo.attach(servoPin);"
// Set new Target every 28.44 seconds (leads to 3 days)
ticker.attach(INTERVAL, setNewTarget); // Interval in seconds
}
void loop() {
unsigned long currentMillis = millis();
blend = fmod(float(currentMillis), (INTERVAL * 1000.0f)) / (INTERVAL * 1000.0f);
current = lerp(ptarget, target, blend);
// Serial.print(ptarget);
// Serial.print(" (");
// Serial.print(int(blend*100));
// Serial.print("%) ");
// Serial.print(target);
// Serial.print(" (");
// Serial.print(int((1.0-blend)*100));
// Serial.print("%) = ");
// Serial.println(current);
setServoNormalized(current);
delay(2);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment