feat: begin working with gpio extender. TO TEST !
This commit is contained in:
parent
2598f523d0
commit
50cf3b3241
@ -1,46 +1,39 @@
|
||||
#include <ESP32Servo.h>
|
||||
#include <Wire.h>
|
||||
#include "Adafruit_VL53L0X.h"
|
||||
#include "PCF8574.h"
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
struct pin {
|
||||
int id;
|
||||
int is_extender;
|
||||
};
|
||||
|
||||
struct tube {
|
||||
float height;
|
||||
float target;
|
||||
struct {
|
||||
Servo hard;
|
||||
int pin;
|
||||
struct pin pin;
|
||||
int last_angle;
|
||||
int equilibrium;
|
||||
} servo;
|
||||
struct {
|
||||
Adafruit_VL53L0X hard;
|
||||
char gpio;
|
||||
char xshut;
|
||||
struct pin gpio;
|
||||
struct pin xshut;
|
||||
} tof;
|
||||
int id;
|
||||
};
|
||||
|
||||
static const int servoPin = 13;
|
||||
|
||||
Servo servo1;
|
||||
const int SDL = 22;
|
||||
const int SDA2 = 21; // useless in the code
|
||||
|
||||
const int minHeight = 50;
|
||||
const int maxHeight = 445;
|
||||
|
||||
int minServo = 0;
|
||||
int maxServo = 180;
|
||||
int lastAngle = -1;
|
||||
|
||||
|
||||
float curHeight = 50;
|
||||
float targetHeight = 250;
|
||||
|
||||
int equilibrium = 0;
|
||||
int error = 0;
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
// Replace with your network credentials
|
||||
const char* ssid = "ESP_ping";
|
||||
@ -63,7 +56,7 @@ const char index_html[] PROGMEM = R"rawliteral(
|
||||
h2 {font-size: 3.0rem;}
|
||||
p {font-size: 3.0rem;}
|
||||
body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
|
||||
.switch {position: relative; display: inline-block; width: 120px; height: 68px}
|
||||
.switch {position: relative; display: inline-block; width: 120px; height: 68px}
|
||||
.switch input {display: none}
|
||||
.slider {background-color: #ccc; border-radius: 6px}
|
||||
.slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 3px}
|
||||
@ -164,12 +157,21 @@ void wifisetup() {
|
||||
server.begin();
|
||||
}
|
||||
|
||||
PCF8574 pcf8574(0x20);
|
||||
|
||||
void change_pin(struct pin pin, int state) {
|
||||
if (pin.is_extender) {
|
||||
pcf8574.digitalWrite(pin, state);
|
||||
} else {
|
||||
digitalWrite(pin, state);
|
||||
}
|
||||
}
|
||||
|
||||
struct tube all_tubes[6] = {0};
|
||||
|
||||
Adafruit_VL53L0X tofs_talk_through[6] = {Adafruit_VL53L0X(), Adafruit_VL53L0X(), Adafruit_VL53L0X(), Adafruit_VL53L0X(), Adafruit_VL53L0X(), Adafruit_VL53L0X()};
|
||||
|
||||
|
||||
void init_tube(struct tube *tube, char id, char servo_pin, char gpio, char xshut) {
|
||||
void init_tube(struct tube *tube, char id, struct pin servo_pin, struct pin gpio, struct pin xshut) {
|
||||
Serial.print(" Initialiazing tube #");
|
||||
Serial.println(id);
|
||||
tube->id = id;
|
||||
@ -179,7 +181,7 @@ void init_tube(struct tube *tube, char id, char servo_pin, char gpio, char xshut
|
||||
tube->height = 250.0;
|
||||
tube->target = 250.0;
|
||||
|
||||
digitalWrite(xshut, HIGH);
|
||||
change_pin(xshut, HIGH);
|
||||
if (!tofs_talk_through[id].begin(0x30 + id)) {
|
||||
Serial.println(F("Failed to boot VL53L0X"));
|
||||
while(1);
|
||||
@ -205,19 +207,40 @@ void init_tube(struct tube *tube, char id, char servo_pin, char gpio, char xshut
|
||||
const int ACTIVES = 1;
|
||||
|
||||
void setup_all_tubes() {
|
||||
const int all_xshuts[6] = {17, 0, 0, 0, 0, 0};
|
||||
const int all_gpios[6] = {23, 22, 21, 0, 0, 0};
|
||||
const int all_servos[6] = {32, 33, 23, 16, 4, 2};
|
||||
const int all_xshuts[6] = {
|
||||
{.id = 17, .is_extender = 0},
|
||||
{.id = P2, .is_extender = 1},
|
||||
{.id = P3, .is_extender = 1},
|
||||
{.id = P5, .is_extender = 1},
|
||||
{.id = P7, .is_extender = 1},
|
||||
{.id = P1, .is_extender = 1}
|
||||
};
|
||||
const int all_gpios[6] = {
|
||||
{.id = 5, .is_extender = 0},
|
||||
{.id = 18, .is_extender = 0},
|
||||
{.id = 19, .is_extender = 0},
|
||||
{.id = P4, .is_extender = 1},
|
||||
{.id = P6, .is_extender = 1},
|
||||
{.id = P0, .is_extender = 1}
|
||||
};
|
||||
const int all_servos[6] = {
|
||||
{.id = 32, .is_extender = 0},
|
||||
{.id = 33, .is_extender = 0},
|
||||
{.id = 23, .is_extender = 0},
|
||||
{.id = 16, .is_extender = 0},
|
||||
{.id = 14, .is_extender = 0},
|
||||
{.id = 2, .is_extender = 0}
|
||||
};
|
||||
for (int i = 0; i < ACTIVES; ++i) {
|
||||
digitalWrite(all_xshuts[i], LOW);
|
||||
change_pin(all_xshuts[i], LOW);
|
||||
}
|
||||
delay(10);
|
||||
for (int i = 0; i < ACTIVES; ++i) {
|
||||
digitalWrite(all_xshuts[i], HIGH);
|
||||
change_pin(all_xshuts[i], HIGH);
|
||||
}
|
||||
delay(10);
|
||||
for (int i = 0; i < ACTIVES; ++i) {
|
||||
digitalWrite(all_xshuts[i], LOW);
|
||||
change_pin(all_xshuts[i], LOW);
|
||||
}
|
||||
for (int i = 0; i < ACTIVES; ++i) {
|
||||
init_tube(all_tubes, i, all_servos[i], all_gpios[i], all_xshuts[i]);
|
||||
@ -230,6 +253,9 @@ void setup() {
|
||||
delay(1);
|
||||
}
|
||||
|
||||
Wire.begin();
|
||||
pcf8574.begin();
|
||||
|
||||
wifisetup();
|
||||
|
||||
setup_all_tubes();
|
||||
@ -246,7 +272,6 @@ int servoPosition(float ratio){
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
for (int i = 0; i < ACTIVES; i++) {
|
||||
struct tube tube = all_tubes[i];
|
||||
tube.height = mesureHeight(tofs_talk_through[tube.id]);
|
||||
@ -272,4 +297,4 @@ void loop() {
|
||||
}
|
||||
|
||||
delay(1);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user