less i2c errors

This commit is contained in:
Pierre Tellier 2025-05-09 08:08:16 +02:00
parent 4cd08211a3
commit ca35d80054

View File

@ -2,21 +2,26 @@
#include <Wire.h> #include <Wire.h>
#include "Adafruit_VL53L0X.h" #include "Adafruit_VL53L0X.h"
#include <Adafruit_PCF8574.h> #include <Adafruit_PCF8574.h>
#include "terminal.hpp" #include <WiFi.h>
// #include <AsyncTCP.h>
// ATTENTION : pour que les commandes se déclarent automatiquement au démarrage #include <ESPAsyncWebServer.h>
// auprès du terminal, le fichier conteant la fonction setup() et loop() doit
// être mis en derniere position dans la liste des fichiers lors de l'édition
// des liens.
// Dans platformio, pour faire cela, il suffit de commencer le nom du fichier
// contenant les fonctions loop() et setup() par la lettre z.
// Par exemple : zzz_main.cpp.
//
extern terminal_n::Terminal terminal; /*
#include "terminal.hpp"
//
// ATTENTION : pour que les commandes se déclarent automatiquement au démarrage
// auprès du terminal, le fichier conteant la fonction setup() et loop() doit
// être mis en derniere position dans la liste des fichiers lors de l'édition
// des liens.
// Dans platformio, pour faire cela, il suffit de commencer le nom du fichier
// contenant les fonctions loop() et setup() par la lettre z.
// Par exemple : zzz_main.cpp.
//
extern terminal_n::Terminal terminal;
TERMINAL_PARAMETER_INT( TERMINAL_PARAMETER_INT(
terminal, terminal,
power_mode, // variable name to declare power_mode, // variable name to declare
"power_mode", // EEPROM name (15 char. max), "power_mode", // EEPROM name (15 char. max),
@ -26,21 +31,23 @@ TERMINAL_PARAMETER_INT(
false, // auto save in eeprom after command modification false, // auto save in eeprom after command modification
terminal_n::filter_nothing, // function to filter the value terminal_n::filter_nothing, // function to filter the value
terminal_n::do_nothing // function executed at the end of the command terminal_n::do_nothing // function executed at the end of the command
); );
TERMINAL_COMMAND(terminal, ping, "Print pong followoing by the received arguments") TERMINAL_COMMAND(terminal, ping, "Print pong followoing by the received arguments")
{ {
terminal.print("pong"); terminal.print("pong");
for (uint32_t i=0; i<argc; i++) { for (uint32_t i=0; i<argc; i++) {
terminal.print(" "); terminal.print(" ");
terminal.print(argv[i]); terminal.print(argv[i]);
} }
terminal.println(""); terminal.println("");
} }
TERMINAL_ADD_DEFAULT_COMMANDS(terminal) // help, params, load_config, reset_config, echo ...
terminal_n::Terminal terminal("Pingpong flottant.\n\r\n\r");
*/
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
struct pin { struct pin {
int id; int id;
@ -64,6 +71,8 @@ struct tube {
int id; int id;
}; };
const int TUBES_COUNT = 6;
struct tube all_tubes[6] = {0}; struct tube all_tubes[6] = {0};
const float minHeight = 50; const float minHeight = 50;
@ -83,8 +92,8 @@ const char* PARAM_INPUT_2 = "state";
AsyncWebServer server(80); AsyncWebServer server(80);
const char index_html[] PROGMEM = R"rawliteral( const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html> <!DOCTYPE HTML><html>
<head> <head>
<title>Balle Ping Pong flottante</title> <title>Balle Ping Pong flottante</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,"> <link rel="icon" href="data:,">
@ -100,24 +109,25 @@ const char index_html[] PROGMEM = R"rawliteral(
input:checked+.slider {background-color: #b30000} input:checked+.slider {background-color: #b30000}
input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)} input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
</style> </style>
</head> </head>
<body> <body>
<h2>Balle Ping Pong flottante</h2> <h2>Balle Ping Pong flottante</h2>
%BUTTONPLACEHOLDER% %BUTTONPLACEHOLDER%
<script> <script>
function sendSliderValue(id, element) { function sendSliderValue(id, element) {
var slider = document.getElementById(`slider${id}`); var slider = document.getElementById(`slider${id}`);
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open("GET", "/update?output="+id+"&state=" + slider.value, true); xhr.open("GET", "/update?output="+id+"&state=" + slider.value, true);
xhr.send(); xhr.send();
} }
</script> </script>
</body> </body>
</html> </html>
)rawliteral"; )rawliteral";
// Replaces placeholder with button section in your web page // Replaces placeholder with button section in your web page
String processor(const String& var){ String processor(const String& var){
//Serial.println(var); //Serial.println(var);
@ -135,6 +145,7 @@ String processor(const String& var){
return String(); return String();
} }
auto mesureHeight(Adafruit_VL53L0X lox, struct tube *tube){ auto mesureHeight(Adafruit_VL53L0X lox, struct tube *tube){
VL53L0X_RangingMeasurementData_t measure; VL53L0X_RangingMeasurementData_t measure;
lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout! lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!
@ -149,11 +160,12 @@ auto mesureHeight(Adafruit_VL53L0X lox, struct tube *tube){
} }
} }
else { else {
Serial.println(" out of range "); //Serial.println(" out of range ");
return tube->height; return tube->height;
} }
} }
void wifisetup() { void wifisetup() {
Serial.print("Setting AP (Access Point)…"); Serial.print("Setting AP (Access Point)…");
WiFi.softAP(ssid); WiFi.softAP(ssid);
@ -202,8 +214,18 @@ void change_pin(struct pin pin, int state) {
} }
} }
void changePinMode(struct pin pin, int state) {
if (pin.is_extender) {
pcf8574.pinMode(pin.id, state);
} else {
pinMode(pin.id, state);
}
}
Adafruit_VL53L0X tofs_talk_through[6] = {Adafruit_VL53L0X(), Adafruit_VL53L0X(), Adafruit_VL53L0X(), Adafruit_VL53L0X(), Adafruit_VL53L0X(), Adafruit_VL53L0X()}; 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, struct pin servo_pin, struct pin gpio, struct pin xshut) { void init_tube(struct tube *tube, char id, struct pin servo_pin, struct pin gpio, struct pin xshut) {
Serial.print(" Initialiazing tube #"); Serial.print(" Initialiazing tube #");
Serial.println((int) id); Serial.println((int) id);
@ -213,21 +235,32 @@ void init_tube(struct tube *tube, char id, struct pin servo_pin, struct pin gpio
tube->tof.xshut = xshut; tube->tof.xshut = xshut;
tube->height = 250.0f; tube->height = 250.0f;
tube->target = 250.0f; tube->target = 250.0f;
change_pin(gpio, HIGH); change_pin(gpio, HIGH);
change_pin(xshut, HIGH); change_pin(xshut, HIGH);
change_pin(servo_pin, HIGH); //change_pin(servo_pin, HIGH);
Serial.println("Beginning boot sequence VL53L0X"); Serial.println("Beginning boot sequence VL53L0X");
if (!tofs_talk_through[id].begin(0x30 + id)) { /*
if (!tofs_talk_through[id].begin()) {
Serial.println(F("Failed to boot VL53L0X")); Serial.println(F("Failed to boot VL53L0X"));
Serial.println((int)id);
while(1); while(1);
}*/
while (!tofs_talk_through[id].begin()) {
Serial.println(F("Failed to boot VL53L0X"));
Serial.println("Adafruit VL53L0X XShut set Low to Force HW Reset");
digitalWrite(xshut.id, LOW);
delay(100);
digitalWrite(xshut.id, HIGH);
Serial.println("Adafruit VL53L0X XShut set high to Allow Boot");
delay(5000);
} }
tofs_talk_through[id].setAddress(0x30 + id);
tube->servo.hard.attach(tube->servo.pin.id); tube->servo.hard.attach(tube->servo.pin.id);
tube->servo.hard.write(minServo); tube->servo.hard.write(minServo);
delay(5000); delay(5000);
Serial.println("Setuping downards"); Serial.println("Setuping downards");
float h = mesureHeight(tofs_talk_through[id], tube); float h = mesureHeight(tofs_talk_through[id], tube);
Serial.println(h);
int angle = minServo; int angle = minServo;
while (h > maxHeight) { while (h > maxHeight) {
++angle; ++angle;
@ -242,22 +275,22 @@ void init_tube(struct tube *tube, char id, struct pin servo_pin, struct pin gpio
Serial.println("Done."); Serial.println("Done.");
} }
const int ACTIVES = 1;
void setup_all_tubes() { void setup_all_tubes() {
const struct pin all_xshuts[6] = { const struct pin all_xshuts[6] = {
{.id = 17, .is_extender = 0}, {.id = 17, .is_extender = 0},
{.id = 2, .is_extender = 1}, {.id = 2, .is_extender = 1},
{.id = 3, .is_extender = 1}, {.id = 3, .is_extender = 1},
{.id = 5, .is_extender = 1},
{.id = 7, .is_extender = 1}, {.id = 7, .is_extender = 1},
{.id = 5, .is_extender = 1},
{.id = 1, .is_extender = 1} {.id = 1, .is_extender = 1}
}; };
const struct pin all_gpios[6] = { const struct pin all_gpios[6] = {
{.id = 5, .is_extender = 0}, {.id = 5, .is_extender = 0},
{.id = 18, .is_extender = 0}, {.id = 18, .is_extender = 0},
{.id = 19, .is_extender = 0}, {.id = 19, .is_extender = 0},
{.id = 4, .is_extender = 1}, {.id = -1, .is_extender = 1},
{.id = 6, .is_extender = 1}, {.id = 6, .is_extender = 1},
{.id = 0, .is_extender = 1} {.id = 0, .is_extender = 1}
}; };
@ -269,25 +302,29 @@ void setup_all_tubes() {
{.id = 14, .is_extender = 0}, {.id = 14, .is_extender = 0},
{.id = 2, .is_extender = 0} {.id = 2, .is_extender = 0}
}; };
for (int i = 0; i < ACTIVES; ++i) {
// Reset all TOFs by turning them up and low
for (int i = 0; i < TUBES_COUNT; ++i) {
changePinMode(all_xshuts[i], OUTPUT);
change_pin(all_xshuts[i], LOW); change_pin(all_xshuts[i], LOW);
} }
delay(10); delay(1000);
for (int i = 0; i < ACTIVES; ++i) { /*
for (int i = 0; i < TUBES_COUNT; ++i) {
change_pin(all_xshuts[i], HIGH); change_pin(all_xshuts[i], HIGH);
} }
delay(10); delay(10);
for (int i = 0; i < ACTIVES; ++i) { for (int i = 0; i < TUBES_COUNT; ++i) {
change_pin(all_xshuts[i], LOW); change_pin(all_xshuts[i], LOW);
} }
for (int i = 0; i < ACTIVES; ++i) { */
for (int i = 0; i < TUBES_COUNT; ++i) {
init_tube(all_tubes, i, all_servos[i], all_gpios[i], all_xshuts[i]); init_tube(all_tubes, i, all_servos[i], all_gpios[i], all_xshuts[i]);
} }
} }
TERMINAL_ADD_DEFAULT_COMMANDS(terminal) // help, params, load_config, reset_config, echo ...
terminal_n::Terminal terminal("Pingpong flottant.\n\r\n\r");
void setup() { void setup() {
@ -308,7 +345,7 @@ void setup() {
wifisetup(); wifisetup();
terminal.setup(&Serial); //terminal.setup(&Serial);
setup_all_tubes(); setup_all_tubes();
@ -325,8 +362,8 @@ int servoPosition(float ratio){
} }
void loop() { void loop() {
terminal.update(); //terminal.update();
for (int i = 0; i < ACTIVES; i++) { for (int i = 0; i < TUBES_COUNT; i++) {
struct tube *tube = all_tubes + i; struct tube *tube = all_tubes + i;
/* /*
Serial.print("Tube "); Serial.print("Tube ");
@ -359,8 +396,15 @@ void loop() {
Serial.print(tube->target); Serial.print(tube->target);
Serial.print("; Angle = "); Serial.print("; Angle = ");
Serial.println(tube->servo.last_angle); Serial.println(tube->servo.last_angle);
*/tube->servo.hard.write(tube->servo.last_angle); */
tube->servo.hard.write(tube->servo.last_angle);
} }
for (int i = 0; i<TUBES_COUNT; i++){
Serial.print((&all_tubes[i])->height);
Serial.print("\t");
}
Serial.println();
delay(1); delay(1);
} }