update folder tree

This commit is contained in:
Clément SAILLANT
2023-09-15 11:27:25 +02:00
committed by GitHub
parent 429724606b
commit 6c3313555f
10 changed files with 1438 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
; 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
[platformio]
description = Digitally Controlled Full Analog Gain Stage
default_envs = uno
[env:uno]
platform = atmelavr
board = uno
framework = arduino
platform_packages = toolchain-atmelavr@3.70300.220127
board_build.mcu = atmega328p
board_build.f_cpu = 16000000L
upload_port = /dev/cu.usbmodem2101
debug_port = /dev/cu.usbmodem2101
debug_build_flags =
-DAVR8_BREAKPOINT_MODE=1
-Og
-g
-fno-lto
lib_deps =
sparkfun/SparkFun MiniMoto@^1.1.0
Wire
dxinteractive/ResponsiveAnalogRead @ ^1.2.1
thomasfredericks/Bounce2 @ ^2.71
robtillaart/PCF8574 @ ^0.3.7
jdolinay/avr-debugger @ ~1.4
mike-matera/FastPID @ ^1.3.1
+169
View File
@@ -0,0 +1,169 @@
/*******************************************************************************************************
gestion des états de boutton
*******************************************************************************************************/
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
// création instance bouton
Bounce2::Button const_out_L = Bounce2::Button(); // instance du bouton const_out_L
Bounce2::Button const_out_R = Bounce2::Button(); // instance du bouton const_out_R
Bounce2::Button stereo_link = Bounce2::Button(); // instance du bouton stereo_link
void button_position_save(); // enregistrement des différences de valeurs
void lecture_switch(); // lecture bouton
void bouton_set(); // gestion des boutons
void stereo_link_set(); // gestion du bouton stereo_link
void const_out_L_set(); // gestion du bouton const_out_L
void const_out_R_set(); // gestion du bouton const_out_R
void button_position_save() // calcul de la différence entre gain et volume
{
if (const_out_L_state == true) // si bouton const_out_L appuyé
{
for (int i = 0; i <= 1; i++)
{
moteur_stop(i); // arrêt des moteurs
lecture_pot(i); // lecture des potentiomètres
position_set[i] = position_lue[i]; // set position potentiomètre gain et volume gauche et droit
position_memory[i] = position_lue[i];
}
}
if (const_out_R_state == true) // si bouton const_out_R appuyé
{
for (int i = 2; i <= 3; i++)
{
moteur_stop(i); // arrêt des moteurs
lecture_pot(i); // lecture des potentiomètres
position_set[i] = position_lue[i]; // set position potentiomètre gain et volume gauche et droit
position_memory[i] = position_lue[i];
}
}
if (stereo_link_state == true) // si bouton stereo_link appuyé
{
for (int i = 0; i <= 3; i++)
{
moteur_stop(i); // arrêt des moteurs
lecture_pot(i); // lecture des potentiomètres
position_set[i] = position_lue[i]; // set position potentiomètre gain et volume gauche et droit
position_memory[i] = position_lue[i];
}
}
}
void lecture_switch()
{
// lecture des boutons
stereo_link.update();
const_out_L.update();
const_out_R.update();
// mise à jour des états des boutons
if (stereo_link.pressed())
{
stereo_link_state = !stereo_link_state;
}
if (const_out_L.pressed())
{
const_out_L_state = !const_out_L_state;
}
if (const_out_R.pressed())
{
const_out_R_state = !const_out_R_state;
}
}
void bouton_set()
{
lecture_switch();
if (stereo_link_state_old != stereo_link_state) // si changement d'état du bouton stereo_link
{
stereo_link_set(); // gestion du bouton stereo_link
state_button_change = true; // il y a changement d'état d'un bouton
stereo_link_state_old = stereo_link_state; // sauvegarde état bouton stereo_link
}
if (const_out_L_state_old != const_out_L_state) // si changement d'état du bouton const_out_L
{
const_out_L_set(); // gestion du bouton const_out_L
state_button_change = true; // il y a changement d'état d'un bouton
const_out_L_state_old = const_out_L_state; // sauvegarde état bouton const_out_L
}
if (const_out_R_state_old != const_out_R_state) // si changement d'état du bouton const_out_R
{
const_out_R_set(); // gestion du bouton const_out_R
state_button_change = true; // il y a changement d'état d'un bouton
const_out_R_state_old = const_out_R_state; // sauvegarde état bouton const_out_R
}
}
void stereo_link_set() // gestion du bouton stereo_link
{
if (stereo_link_state == true) // si bouton stereo_link appuyé
{
digitalWrite(stereo_link_led, HIGH); // allume LED stereo_link
if (const_out_L_state == true || const_out_R_state == true) // si bouton const_out_L ou R appuyé
{
const_out_L_state = stereo_link_state;
const_out_R_state = stereo_link_state;
}
}
else // si bouton stereo_link relaché
{
digitalWrite(stereo_link_led, LOW); // éteint LED stereo_link
// sécurité d'arrêt des moteurs
for (int i = 0; i <= 3; i++) // pour chaque moteur
{
motor[i].brake(); // frein moteur
delay(5);
motor[i].stop(); // arrêt moteur
}
}
}
void const_out_L_set() // gestion du bouton const_out_L
{
if (const_out_L_state == true) // si bouton const_out_L appuyé
{
digitalWrite(const_out_L_led, HIGH); // allume LED const_out_L
if (stereo_link_state == true) // si bouton stereo_link appuyé
{
const_out_R_state = true;
}
}
else if (stereo_link_state != true) // si bouton const_out_L relaché et pas de stereo_link
{
digitalWrite(const_out_L_led, LOW); // éteint LED const_out_L
// sécurité d'arrêt des moteurs
for (int i = 0; i <= 1; i++) // pour chaque moteur
{
motor[i].brake(); // frein moteur
delay(5);
motor[i].stop(); // arrêt moteur
}
}
}
void const_out_R_set() // gestion du bouton const_out_R
{
if (const_out_R_state == true) // si bouton const_out_R appuyé
{
digitalWrite(const_out_R_led, HIGH); // allume LED const_out_R
if (stereo_link_state == true) // si bouton stereo_link appuyé
{
const_out_L_state = true;
}
}
else if (stereo_link_state != true) // si bouton const_out_L relaché et pas de stereo_link
{
digitalWrite(const_out_R_led, LOW); // éteint LED const_out_R
// sécurité d'arrêt des moteurs
for (int i = 2; i <= 3; i++) // pour chaque moteur
{
motor[i].brake(); // frein moteur
delay(5);
motor[i].stop(); // arrêt moteur
}
}
}
+115
View File
@@ -0,0 +1,115 @@
/*******************************************************************************************************
Gestion de la calibration des min et max des potentiomètres et stockage dans l'EEPROM
********************************************************************************************************/
#define speed 20
#define interval_cal 3000
void calibration_pot(int i); // definition fonction de calibration des potentiomètres
void calibration_pot(int i) // calibration des potentiomètres
{
if (analogRead(analog_pot[i]) > 800)
{
motor[i].drive(63);
delay(1000);
}
if (analogRead(analog_pot[i]) < 200)
{
motor[i].drive(-63);
delay(1000);
}
// *******************************************************************************************************
// calibration du min des potentiomètres
digitalWrite(stereo_link_led, HIGH); // allume LED stereo_link
while (analogRead(analog_pot[i]) < 950) // en analogue read les valeurs sont inversées
{
delay(200);
digitalWrite(const_out_L_led, HIGH);
digitalWrite(const_out_R_led, LOW);
motor[i].drive(-63);
delay(200);
digitalWrite(const_out_L_led, LOW);
digitalWrite(const_out_R_led, HIGH);
}
motor[i].drive(-speed);
delay(interval_cal);
motor[i].stop();
delay(500);
digitalWrite(stereo_link_led, LOW); // éteint LED stereo_link
switch (i)
{
case 0:
gain_0.update();
max_pot[i] = gain_0.getValue();
break;
case 1:
vol_0.update();
max_pot[i] = vol_0.getValue();
break;
case 2:
gain_1.update();
max_pot[i] = gain_1.getValue();
break;
case 3:
vol_1.update();
max_pot[i] = vol_1.getValue();
break;
}
EEPROM.write(i + 10, max_pot[i] / 4);
delay(500);
// *******************************************************************************************************
// calibration du max des potentiomètres
digitalWrite(stereo_link_led, HIGH); // éteint LED stereo_link
while (analogRead(analog_pot[i]) > 50) // en analogue read les valeurs sont inversées
{
delay(200);
digitalWrite(const_out_L_led, LOW);
digitalWrite(const_out_R_led, HIGH);
motor[i].drive(63);
delay(200);
digitalWrite(const_out_L_led, HIGH);
digitalWrite(const_out_R_led, LOW);
}
motor[i].drive(speed);
delay(interval_cal);
motor[i].stop();
delay(500);
digitalWrite(stereo_link_led, LOW); // éteint LED stereo_link
switch (i)
{
case 0:
gain_0.update();
min_pot[i] = gain_0.getValue();
break;
case 1:
vol_0.update();
min_pot[i] = vol_0.getValue();
break;
case 2:
gain_1.update();
min_pot[i] = gain_1.getValue();
break;
case 3:
vol_1.update();
min_pot[i] = vol_1.getValue();
break;
}
EEPROM.write(i, min_pot[i] / 4);
delay(500);
digitalWrite(stereo_link_led, HIGH); // éteint LED stereo_link
// remise à zéro du potentiomètres
digitalWrite(stereo_link_led, HIGH); // allume LED stereo_link
while (analogRead(analog_pot[i]) < max_pot[i]) // en analogue read les valeurs sont inversées
{
delay(200);
digitalWrite(stereo_link_led, HIGH); // allume LED stereo_link
motor[i].drive(-63);
delay(200);
digitalWrite(stereo_link_led, LOW); // allume LED stereo_link
}
motor[i].stop();
delay(500);
}
+171
View File
@@ -0,0 +1,171 @@
/*******************************************************************************************************
gestion des consigne et des calcul de position des moteurs
*******************************************************************************************************/
void consigne_set(int i); // calcul des consignes
void motor_calc(int pot, int motor); // calcul des position de moteurs
void position_const_calc(int pot, int motor); // calcul de la consigne de position
void position_stereo_calc(int pot, int motor); // calcul de la consigne de position en stereo
void motor_calc(int pot, int motor) // calcul des consignes de moteurs et du flag de changement de position
{
lecture_pot(pot); // lecture des potentiomètres
lecture_pot(motor); // lecture des potentiomètres
if (position_set[motor] <= 0)
{
position_set[motor] = min_pot[motor];
}
else if (position_set[motor] >= 1023)
{
position_set[motor] = max_pot[motor];
}
if (position_lue[motor] <= (position_set[motor] + ECART_V_STOP) && position_lue[motor] >= (position_set[motor] - ECART_V_STOP)) // si moteur entre consigne et ECART_V_STOP
{
motor_change[motor] = false; // moteur entre consigne et ECART_V_STOP
}
else
{
motor_change[motor] = true; // il y a changement de position du potentiomètre
}
}
void position_const_calc(int pot, int motor)
{
if (position_lue[pot] < position_memory[pot] - ECART_V_STOP)
{
position_set[motor] = position_memory[motor] + (position_memory[pot] - position_lue[pot]);
motor_calc(pot, motor); // pot, motor
}
else if (position_lue[pot] > position_memory[pot] + ECART_V_STOP)
{
position_set[motor] = position_memory[motor] - (position_lue[pot] - position_memory[pot]);
motor_calc(pot, motor); // pot, motor
}
else
{
return;
}
}
void position_stereo_const_calc(int pot, int motor)
{
if (position_set[pot] < position_memory[pot] - ECART_V_STOP)
{
position_set[motor] = position_memory[motor] + (position_memory[pot] - position_set[pot]);
motor_calc(pot, motor); // pot, motor
}
else if (position_set[pot] > position_memory[pot] + ECART_V_STOP)
{
position_set[motor] = position_memory[motor] - (position_set[pot] - position_memory[pot]);
motor_calc(pot, motor); // pot, motor
}
else
{
return;
}
}
void position_stereo_calc(int pot, int motor)
{
if (position_lue[pot] < position_memory[pot] - ECART_V_STOP)
{
position_set[motor] = position_memory[motor] - (position_memory[pot] - position_lue[pot]);
motor_calc(pot, motor); // pot, motor
}
else if (position_lue[pot] > position_memory[pot] + ECART_V_STOP)
{
position_set[motor] = position_memory[motor] + (position_lue[pot] - position_memory[pot]);
motor_calc(pot, motor); // pot, motor
}
else
{
return;
}
}
void consigne_set(int i) // calcul des consignes de moteurs
{
if (stereo_link_state == true) // si stereo_link actif
{
switch (i)
{
case 0:
position_stereo_calc(0, 2); // pot, motor
break;
case 1:
position_stereo_calc(1, 3); // pot, motor
break;
case 2:
position_stereo_calc(2, 0); // pot, motor
break;
case 3:
position_stereo_calc(3, 1); // pot, motor
break;
}
}
if (const_out_L_state == true) // si bouton const_out_L ou R appuyé
{
const int gain_const = 0;
const int volume_const = 1;
const int gain_norm = 2;
const int volume_norm = 3;
switch (i)
{
case gain_const:
position_const_calc(gain_const, volume_const);
if (stereo_link_state == true)
{
position_stereo_const_calc(gain_norm, volume_norm);
}
break;
case volume_const:
position_const_calc(volume_const, gain_const);
if (stereo_link_state == true)
{
position_stereo_const_calc(volume_norm, gain_norm);
}
break;
default:
break;
}
if (stereo_link_state != true)
position_set[i] = position_lue[i];
#ifdef DEBUG // si DEBUG activé
breakpoint();
#endif
}
if (const_out_R_state == true) // si const_out_L actif
{
const int gain_const = 2;
const int volume_const = 3;
const int gain_norm = 0;
const int volume_norm = 1;
switch (i)
{
case gain_const:
position_const_calc(gain_const, volume_const);
if (stereo_link_state == true)
{
position_stereo_const_calc(gain_norm, volume_norm);
}
break;
case volume_const:
position_const_calc(volume_const, gain_const);
if (stereo_link_state == true)
{
position_stereo_const_calc(volume_norm, gain_norm);
}
break;
default:
break;
}
if (stereo_link_state != true)
position_set[i] = position_lue[i];
#ifdef DEBUG // si DEBUG activé
breakpoint();
#endif
}
}
+166
View File
@@ -0,0 +1,166 @@
/*
Hlabs controle de volume et de gain pour préampli stéréo avec fonction de contrôle constant output
https://www.hlabs.audio
https://www.facebook.com/hlabs.audio
contact@hlabs.audio
code par Clément SAILLANT pour Hlabs Audio- 2023
c.saillant@gmail.com
0625334420
*/
#include <Arduino.h>
#include <EEPROM.h>
// *******************************************************************************************************
// ******************** pour activer ou non le DEBUG *****************************************************
// ******************** // #define DEBUG = INACTIF *******************************************************
// ******************** #define DEBUG = ACTIF ************************************************************
// *******************************************************************************************************
// #define DEBUG
#ifdef DEBUG // si DEBUG activé
#include "avr8-stub.h" // avr debug stub
#include "app_api.h" // only needed with flash breakpoints
#endif
// code pour breakpoint :
/*
#ifdef DEBUG // si DEBUG activé
breakpoint();
#endif
*/
// *******************************************************************************************************
// ****************************************** include ***************************************************
// *******************************************************************************************************
#include "variables.h"
#include "pot.h"
#include "moteur.h"
#include "calibration.h"
#include "bouton.h"
#include "relais.h"
#include "valeurs.h"
#include "consigne.h"
// *******************************************************************************************************
// ****************************************** boucle setup ***********************************************
// *******************************************************************************************************
void setup()
{
#ifdef DEBUG // si DEBUG activé
// initialize GDB stub
debug_init();
#endif
// set bouton const_out_L
pinMode(const_out_sw_L, INPUT_PULLUP);
pinMode(const_out_L_led, OUTPUT);
const_out_L.attach(const_out_sw_L);
const_out_L.interval(interval_button);
const_out_L.setPressedState(LOW);
// set bouton const_out_R
pinMode(const_out_sw_R, INPUT_PULLUP);
pinMode(const_out_R_led, OUTPUT);
const_out_R.attach(const_out_sw_R);
const_out_R.interval(interval_button);
const_out_R.setPressedState(LOW);
// set bouton stereo_link
pinMode(stereo_link_sw, INPUT_PULLUP);
pinMode(stereo_link_led, OUTPUT);
stereo_link.attach(stereo_link_sw);
stereo_link.interval(interval_button);
stereo_link.setPressedState(LOW);
// set motor stop
for (int i = 0; i <= 3; i++)
{
motor[i].stop();
delay(50);
lecture_pot(i); // lecture analogique potentiomètre
valeurs_set(i); // controle des valeurs et des relais
save_pot(i); // sauvegarde position potentiomètre
position_set[i] = position_lue[i];
}
// calibration des potentiomètres avec bouton stereo_link enfoncé au démarrage
if (digitalRead(stereo_link_sw) == LOW)
{
for (int i = 0; i <= 3; i++)
{
calibration_pot(i);
}
digitalWrite(const_out_L_led, LOW);
digitalWrite(const_out_R_led, LOW);
digitalWrite(stereo_link_led, LOW);
stereo_link_state = false; // initialisation de l'état du bouton stereo_link en cas de démarrage avec le bouton enfoncé pour la calibration
}
// lecture des valeurs de calibration enregistrées dans la mémoire EEPROM
for (int i = 0; i <= 3; i++)
{
max_pot[i] = EEPROM.read(i + 10) * 4;
if (max_pot[i] == 0)
{
max_pot[i] = 1023;
}
min_pot[i] = (EEPROM.read(i) * 4) + 1;
}
#ifdef DEBUG // si DEBUG activé
breakpoint();
#endif
bouton_set(); // lecture et controle des boutons
}
// *******************************************************************************************************
// ****************************************** boucle principale ******************************************
// *******************************************************************************************************
void loop()
{
bouton_set(); // lecture et controle des boutons
if (state_button_change == true) // si changement d'état d'un bouton
{
button_position_save(); // enregistrement de position des potentiomètres
state_button_change = false; // remise à zéro du flag de changement de bouton
}
// *******************************************************************************************************
// ********************************* boucle de lecture des potentiomètres ********************************
// *******************************************************************************************************
for (int i = 0; i <= 3; i++)
{
// *******************************************************************************************************
// ********************************* boucle de controle des boutons **************************************
// *******************************************************************************************************
lecture_pot(i); // lecture analogique potentiomètre avec mise à jour du flag de changement de potentiomètre
if (position_change[i] == true) // si changement de position d'un potentiomètre
{
if (stereo_link_state != true && const_out_L_state != true && const_out_R_state != true) // si pas de contrôle constant output
{
valeurs_set(i); // controle des valeurs et des relais en mode normal
}
// si contrôle constant output ou stereo link avec moteur arrêté il y a plus de bounce_time_pot et mouvement supérieur à ECART_V_STOP
else if (motor_change[i] != true && millis() > last_motor_end[i] + bounce_time_pot)
{
if (abs(position_lue[i] - position_set[i]) >= ECART_V_STOP)
{
consigne_set(i); // mise à jour des consignes
for (int i = 0; i <= 3; i++)
{
valeurs_const_set(i); // controle des valeurs et des relais en mode constant output
}
}
position_set[i] = position_lue[i];
}
save_pot(i); // sauvegarde position potentiomètre
}
if (motor_change[i] == true) // si moteur en mouvement
{
moteur_set(i); // controle du moteurs
}
}
if (error_state == true)
{
error_led();
}
}
+117
View File
@@ -0,0 +1,117 @@
/*******************************************************************************************************
gestion de la vitesse et du sens de déplacement des moteur
*******************************************************************************************************/
#include <SparkFunMiniMoto.h> // Include the MiniMoto library
/*
Attempts to set speed lower than 6 will be ignored; speeds higher than 63 will be truncated to 63. The sign of the value determines the direction of the motion.
*/
MiniMoto motor[4] = {gain_0_motor, vol_0_motor, gain_1_motor, vol_1_motor}; // Create MiniMoto instances, pour controle des moteur en I2C (DRV8830)
#define ECART_V_STOP 3 // hystérésie de positionnement potentiomètre
#define ECART_V_SPEED_PID 50 // valeur d'écart pour vitesse moteur avec ou sans PID
#define ECART_V_MIN_MAX 10 // valeur d'écart au min et au max pour vitesse moteur
#define speed_min 10 // vitesse moteur sans PID
#define PID_speed_min 15 // vitesse min moteur avec PID
#define PID_speed_max 63 // vitesse max moteur avec PID
#include <FastPID.h>
float Kp = 0.1, Ki = 0.5, Kd = 0, Hz = 2;
int output_bits = 8;
bool output_signed = true;
FastPID myPID(Kp, Ki, Kd, Hz, output_bits, output_signed);
void moteur_set(int i); // fonction de gestion de vitesse et position moteurs
void moteur_stop(int i); // fonction d'arret moteurs
void moteur_stop(int i)
{
myPID.clear(); // remise à zéro du PID
motor_speed[i] = 0; // remise à zéro de la vitesse du moteur
motor[i].stop(); // stop moteur
//delay(100); // attente 100ms
motor_change[i] = false; // remise à zéro du flag de changement de position du potentiomètre
lecture_pot(i); // lecture analogique du potentiomètre
position_set[i] = position_lue[i]; // remise à zéro de la consigne de position du potentiomètre
save_pot(i); // sauvegarde position potentiomètre
last_motor_end[i] = millis();
#ifdef DEBUG // si DEBUG activé
breakpoint();
#endif
}
void moteur_set(int i)
{
if (position_lue[i] <= (position_set[i] + ECART_V_STOP) && position_lue[i] >= (position_set[i] - ECART_V_STOP)) // si moteur entre consigne et ECART_V_STOP
{
motor[i].brake(); // frein moteur
error_state = false; // remise à zéro du flag d'erreur
moteur_stop(i); // stop moteur
return;
}
// *******************************************************************************************************
else if (position_lue[i] > position_set[i]) // si moteur doit aller vers la gauche
{
myPID.configure(Kp, Ki, Kd, Hz, output_bits, output_signed); // configuration PID
myPID.setOutputRange(-PID_speed_max, -PID_speed_min); // plage de sortie du PID
error_state = false; // remise à zéro du flag d'erreur
if (position_lue[i] >= position_set[i] + ECART_V_SPEED_PID) // vitesse PID si moteur n'est pas entre consigne et ECART_V_SPEED_PID
{
motor_speed[i] = myPID.step(position_set[i], position_lue[i]); // vitesse moteur par PID
}
else if (position_lue[i] < position_set[i] + ECART_V_SPEED_PID && position_lue[i] > position_set[i] + ECART_V_STOP)
{
// motor[i].brake(); // frein moteur pour inertie moteur
motor_speed[i] = -speed_min; // vitesse min moteur
myPID.clear(); // remise à zéro du PID
}
else
{
motor[i].brake(); // frein moteur pour inertie moteur
moteur_stop(i); // stop moteur
}
if (position_set[i] <= min_pot[i] + ECART_V_MIN_MAX) // si consigne de position du potentiomètre = 0
{
position_set[i] = min_pot[i];
motor_speed[i] = -63;
}
motor[i].drive(motor_speed[i]); // controle moteur
}
// *******************************************************************************************************
else if (position_lue[i] < position_set[i]) // si moteur doit aller vers la droite
{
myPID.configure(Kp, Ki, Kd, Hz, output_bits, output_signed); // configuration PID
myPID.setOutputRange(PID_speed_min, PID_speed_max); // plage de sortie du PID
error_state = false; // remise à zéro du flag d'erreur
if (position_lue[i] <= position_set[i] - ECART_V_SPEED_PID) // vitesse PID si moteur n'est pas entre consigne et ECART_V_SPEED_PID
{
motor_speed[i] = myPID.step(position_set[i], position_lue[i]); // vitesse moteur par PID
}
else if (position_lue[i] > position_set[i] - ECART_V_SPEED_PID && position_lue[i] < position_set[i] - ECART_V_STOP)
{
// motor[i].brake(); // frein moteur pour inertie moteur
motor_speed[i] = speed_min; // vitesse min moteur
myPID.clear(); // remise à zéro du PID
}
else
{
motor[i].brake(); // frein moteur pour inertie moteur
moteur_stop(i); // stop moteur
}
if (position_set[i] >= max_pot[i] - ECART_V_MIN_MAX) // si consigne de position du potentiomètre = 0
{
position_set[i] = max_pot[i];
motor_speed[i] = 63;
}
motor[i].drive(motor_speed[i]); // controle moteur
}
else
{
error_state = true; // flag d'erreur
motor[i].brake(); // frein moteur
moteur_stop(i);
return;
}
}
+82
View File
@@ -0,0 +1,82 @@
/*******************************************************************************************************
Gestion de la lecture des potentiomètres et des boutons
********************************************************************************************************/
// make a ResponsiveAnalogRead object, pass in the pin, and either true or false depending on if you want sleep enabled
// enabling sleep will cause values to take less time to stop changing and potentially stop changing more abruptly,
// where as disabling sleep will cause values to ease into their correct position smoothly and with slightly greater accuracy
// the next optional argument is snapMultiplier, which is set to 0.01 by default
// you can pass it a value from 0 to 1 that controls the amount of easing
// increase this to lessen the amount of easing (such as 0.1) and make the responsive values more responsive
// but doing so may cause more noise to seep through if sleep is not enabled
#include <ResponsiveAnalogRead.h>
#define amount_easing 0.8 // valeur d'atténuation du potentiomètre
ResponsiveAnalogRead gain_0(gain_0_pot, true, amount_easing);
ResponsiveAnalogRead vol_0(vol_0_pot, true, amount_easing);
ResponsiveAnalogRead gain_1(gain_1_pot, true, amount_easing);
ResponsiveAnalogRead vol_1(vol_1_pot, true, amount_easing);
void save_pot(int i); // sauvegarde position potentiomètre
void lecture_pot(int i); // lecture analogique potentiomètre
void save_pot(int i)
{
position_save[i] = position_lue[i]; // sauvegarde position potentiomètre
position_change[i] = false; // RAZ flag de changement de position du potentiomètre
}
// *******************************************************************************************************
// ****************************************** lecture bouton *********************************************
// *******************************************************************************************************
void lecture_pot(int i)
{
switch (i)
{
case 0:
gain_0.update();
if (gain_0.hasChanged())
{
position_change[i] = true; // il y a changement de position du potentiomètre
int value = gain_0.getValue();
int map_value = map(value, max_pot[i], min_pot[i], 0, 1023);
position_lue[i] = constrain(map_value, 0, 1023);
}
break;
case 1:
vol_0.update();
if (vol_0.hasChanged())
{
position_change[i] = true; // il y a changement de position du potentiomètre
int value = vol_0.getValue();
int map_value = map(value, max_pot[i], min_pot[i], 0, 1023);
position_lue[i] = constrain(map_value, 0, 1023);
}
break;
case 2:
gain_1.update();
if (gain_1.hasChanged())
{
position_change[i] = true; // il y a changement de position du potentiomètre
int value = gain_1.getValue();
int map_value = map(value, max_pot[i], min_pot[i], 0, 1023);
position_lue[i] = constrain(map_value, 0, 1023);
}
break;
case 3:
vol_1.update();
if (vol_1.hasChanged())
{
position_change[i] = true; // il y a changement de position du potentiomètre
int value = vol_1.getValue();
int map_value = map(value, max_pot[i], min_pot[i], 0, 1023);
position_lue[i] = constrain(map_value, 0, 1023);
}
break;
}
}
+92
View File
@@ -0,0 +1,92 @@
/*******************************************************************************************************
gestion des valeurs des relais avec incrément et décrément et de l'envoie des valeurs sur les cartes relais
*******************************************************************************************************/
#include "PCF8574.h" // https://github.com/RobTillaart/PCF8574/blob/master/examples/PCF8574_test1/PCF8574_test1.ino
// instance carte relais
PCF8574 PCF_vol_0(vol_0_relais);
PCF8574 PCF_gain_0(gain_0_relais);
PCF8574 PCF_vol_1(vol_1_relais);
PCF8574 PCF_gain_1(gain_1_relais);
void relais_set(int relais); // fonction de mappage des valeur pour envoie sur les cartes relais
void relais_send(int relais, int value, int byte_value); // fonction d'envoie de valeur sur les cartes relais
void relais_set(int relais)
{
// mise à jour du tableau de valeur des relais
switch (relais)
{
case 0:
relais_map[0] = relais_gain_val[0];
break;
case 1:
relais_map[1] = relais_vol_val[0];
break;
case 2:
relais_map[2] = relais_gain_val[1];
break;
case 3:
relais_map[3] = relais_vol_val[1];
break;
}
if (relais_map[relais] != relais_old[relais]) // si changement de valeur
{
// si changement en sens montant
if (relais_map[relais] > relais_old[relais])
{
for (int j = 0; j <= 7; j++)
{
if (bitRead(relais_map[relais], j) == 1)
{
relais_send(relais, 1, j);
}
else if (bitRead(relais_map[relais], j) == 0)
{
relais_send(relais, 0, j);
}
}
}
// si changement en sens descendant
if (relais_map[relais] < relais_old[relais])
{
for (int j = 7; j >= 0; j--)
{
if (bitRead(relais_map[relais], j) == 1)
{
relais_send(relais, 1, j);
}
else if (bitRead(relais_map[relais], j) == 0)
{
relais_send(relais, 0, j);
}
}
}
relais_old[relais] = relais_map[relais]; // sauvegarde valeur de comparaison
}
}
void relais_send(int relais, int value, int byte_value)
{
// PCF.write(const uint8_t pin, const uint8_t value) writes a single pin; pin = 0..7; value is HIGH(1) or LOW (0)
// uint8_t write(const uint8_t value) writes all pins; value = 0..255
// envoie I2C des valeur sur les cartes relais
switch (relais)
{
case 0:
PCF_gain_0.write(byte_value, value); // carte relais gain gauche
break;
case 1:
PCF_vol_0.write(byte_value, value); // carte relais volume gauche
break;
case 2:
PCF_gain_1.write(byte_value, value); // carte relais gain droit
break;
case 3:
PCF_vol_1.write(byte_value, value); // carte relais volume droit
break;
}
delay(delay_relais);
}
+389
View File
@@ -0,0 +1,389 @@
/*******************************************************************************************************
gestion des valeurs selon position de gain et volume, lissage des valeurs et mise à jour des relais
*******************************************************************************************************/
void valeurs_set(int i); // gestion des valeurs
void valeurs_const_set(int i); // gestion des valeurs constant ouptut
void smoothgain_set(int i); // calcul des valeurs lissées de gain et mise à jour des relais
void smoothvolume_set(int i); // calcul des valeurs lissées de volume et mise à jour des relais
void valeurs_set(int i)
{
if (position_change[i] == true && position_lue[i] != position_save[i]) // si changement d'état du potentiomètre
{
switch (i)
{
case 0:
smoothgain[0] = byte(position_lue[0] / 4); // adaptation de valeur
smoothgain_set(0); // mise à jour de valeur du relais gain gauche
break;
case 1:
smoothvol[0] = byte(position_lue[1] / 4) - 1; // adaptation de valeur
smoothvolume_set(0);
break;
case 2:
smoothgain[1] = byte(position_lue[2] / 4); // adaptation de valeur
smoothgain_set(1); // mise à jour de valeur du relais gain droit
break;
case 3:
smoothvol[1] = byte(position_lue[3] / 4) - 1; // adaptation de valeur
smoothvolume_set(1); // mise à jour de valeur du relais volume droit
break;
default:
break;
}
}
}
void valeurs_const_set(int i)
{
if (position_set[i] != position_save[i]) // si changement d'état du potentiomètre
{
switch (i)
{
case 0:
smoothgain[0] = byte(position_set[0] / 4); // adaptation de valeur
smoothgain_set(0); // mise à jour de valeur du relais gain gauche
break;
case 1:
smoothvol[0] = byte(position_set[1] / 4) - 1; // adaptation de valeur
smoothvolume_set(0); // mise à jour de valeur du relais volume gauche
break;
case 2:
smoothgain[1] = byte(position_set[2] / 4); // adaptation de valeur
smoothgain_set(1); // mise à jour de valeur du relais gain droit
break;
case 3:
smoothvol[1] = byte(position_set[3] / 4) - 1; // adaptation de valeur
smoothvolume_set(1); // mise à jour de valeur du relais volume droit
break;
default:
break;
}
}
}
void smoothgain_set(int i)
{
// lisages manuel des valeurs de gain
smoothgain[i] = map(smoothgain[i], 0, 255, 0, 156); // 156sm
{
relais_gain_val[i] = smoothgain[i];
}
if (smoothgain[i] <= 7 && smoothgain[i] >= 0)
{
relais_gain_val[i] = smoothgain[i];
}
if (smoothgain[i] <= 14 && smoothgain[i] >= 8)
{
relais_gain_val[i] = smoothgain[i] + 1;
}
if (smoothgain[i] <= 18 && smoothgain[i] >= 15)
{
relais_gain_val[i] = smoothgain[i] + 3; //+4
}
if (smoothgain[i] == 19)
{
relais_gain_val[i] = smoothgain[i] + 4;
}
if (smoothgain[i] <= 23 && smoothgain[i] >= 20)
{
relais_gain_val[i] = smoothgain[i] + 5;
}
if (smoothgain[i] <= 25 && smoothgain[i] >= 24)
{
relais_gain_val[i] = smoothgain[i] + 6;
}
if (smoothgain[i] <= 28 && smoothgain[i] >= 26)
{
relais_gain_val[i] = smoothgain[i] + 7;
}
if (smoothgain[i] <= 31 && smoothgain[i] >= 29)
{
relais_gain_val[i] = smoothgain[i] + 8;
}
if (smoothgain[i] <= 35 && smoothgain[i] >= 32)
{
relais_gain_val[i] = smoothgain[i] + 9;
}
if (smoothgain[i] <= 37 && smoothgain[i] >= 36)
{
relais_gain_val[i] = smoothgain[i] + 10;
}
if (smoothgain[i] == 38)
{
relais_gain_val[i] = smoothgain[i] + 11;
}
if (smoothgain[i] <= 40 && smoothgain[i] >= 39)
{
relais_gain_val[i] = smoothgain[i] + 12;
}
if (smoothgain[i] <= 42 && smoothgain[i] >= 41)
{
relais_gain_val[i] = smoothgain[i] + 13;
}
if (smoothgain[i] <= 44 && smoothgain[i] >= 43)
{
relais_gain_val[i] = smoothgain[i] + 14;
}
if (smoothgain[i] <= 46 && smoothgain[i] >= 45)
{
relais_gain_val[i] = smoothgain[i] + 15;
}
if (smoothgain[i] == 47)
{
relais_gain_val[i] = smoothgain[i] + 16;
}
if (smoothgain[i] <= 58 && smoothgain[i] >= 48)
{
relais_gain_val[i] = smoothgain[i] + 18;
}
if (smoothgain[i] <= 60 && smoothgain[i] >= 59)
{
relais_gain_val[i] = smoothgain[i] + 18;
}
if (smoothgain[i] <= 63 && smoothgain[i] >= 61)
{
relais_gain_val[i] = smoothgain[i] + 19;
}
if (smoothgain[i] <= 66 && smoothgain[i] >= 64)
{
relais_gain_val[i] = smoothgain[i] + 20;
}
if (smoothgain[i] <= 68 && smoothgain[i] >= 67)
{
relais_gain_val[i] = smoothgain[i] + 21;
}
if (smoothgain[i] <= 70 && smoothgain[i] >= 69)
{
relais_gain_val[i] = smoothgain[i] + 22;
}
if (smoothgain[i] <= 72 && smoothgain[i] >= 71)
{
relais_gain_val[i] = smoothgain[i] + 23;
}
if (smoothgain[i] <= 74 && smoothgain[i] >= 73)
{
relais_gain_val[i] = smoothgain[i] + 24;
}
if (smoothgain[i] <= 76 && smoothgain[i] >= 75)
{
relais_gain_val[i] = smoothgain[i] + 25;
}
if (smoothgain[i] <= 78 && smoothgain[i] >= 77)
{
relais_gain_val[i] = smoothgain[i] + 26;
}
if (smoothgain[i] <= 80 && smoothgain[i] >= 79)
{
relais_gain_val[i] = smoothgain[i] + 27;
}
if (smoothgain[i] == 81)
{
relais_gain_val[i] = smoothgain[i] + 28;
}
if (smoothgain[i] == 82)
{
relais_gain_val[i] = smoothgain[i] + 29;
}
if (smoothgain[i] <= 84 && smoothgain[i] >= 83)
{
relais_gain_val[i] = smoothgain[i] + 30;
}
if (smoothgain[i] <= 86 && smoothgain[i] >= 85)
{
relais_gain_val[i] = smoothgain[i] + 31;
}
if (smoothgain[i] == 87)
{
relais_gain_val[i] = smoothgain[i] + 32;
}
if (smoothgain[i] == 88)
{
relais_gain_val[i] = smoothgain[i] + 33;
}
if (smoothgain[i] <= 90 && smoothgain[i] >= 89)
{
relais_gain_val[i] = smoothgain[i] + 34;
}
if (smoothgain[i] <= 104 && smoothgain[i] >= 91)
{
relais_gain_val[i] = smoothgain[i] + 35;
}
if (smoothgain[i] <= 107 && smoothgain[i] >= 105)
{
relais_gain_val[i] = smoothgain[i] + 36;
}
if (smoothgain[i] <= 110 && smoothgain[i] >= 108)
{
relais_gain_val[i] = smoothgain[i] + 37;
}
if (smoothgain[i] <= 113 && smoothgain[i] >= 111)
{
relais_gain_val[i] = smoothgain[i] + 38;
}
if (smoothgain[i] <= 115 && smoothgain[i] >= 114)
{
relais_gain_val[i] = smoothgain[i] + 39;
}
if (smoothgain[i] <= 118 && smoothgain[i] >= 116)
{
relais_gain_val[i] = smoothgain[i] + 40;
}
if (smoothgain[i] == 119)
{
relais_gain_val[i] = smoothgain[i] + 42;
}
if (smoothgain[i] <= 121 && smoothgain[i] >= 120)
{
relais_gain_val[i] = smoothgain[i] + 43;
}
if (smoothgain[i] == 122)
{
relais_gain_val[i] = smoothgain[i] + 44;
}
if (smoothgain[i] <= 124 && smoothgain[i] >= 123)
{
relais_gain_val[i] = smoothgain[i] + 45;
}
if (smoothgain[i] <= 126 && smoothgain[i] >= 125)
{
relais_gain_val[i] = smoothgain[i] + 46;
}
if (smoothgain[i] <= 128 && smoothgain[i] >= 127)
{
relais_gain_val[i] = smoothgain[i] + 47;
}
if (smoothgain[i] == 129)
{
relais_gain_val[i] = smoothgain[i] + 48;
}
if (smoothgain[i] == 130)
{
relais_gain_val[i] = smoothgain[i] + 49;
}
if (smoothgain[i] == 131)
{
relais_gain_val[i] = smoothgain[i] + 50;
}
if (smoothgain[i] == 132)
{
relais_gain_val[i] = smoothgain[i] + 51;
}
if (smoothgain[i] <= 134 && smoothgain[i] >= 133)
{
relais_gain_val[i] = smoothgain[i] + 52;
}
if (smoothgain[i] == 135)
{
relais_gain_val[i] = smoothgain[i] + 53;
}
if (smoothgain[i] == 136)
{
relais_gain_val[i] = smoothgain[i] + 54;
}
if (smoothgain[i] == 137)
{
relais_gain_val[i] = smoothgain[i] + 60;
}
if (smoothgain[i] == 138)
{
relais_gain_val[i] = smoothgain[i] + 61;
}
if (smoothgain[i] == 139)
{
relais_gain_val[i] = smoothgain[i] + 63;
}
if (smoothgain[i] == 140)
{
relais_gain_val[i] = smoothgain[i] + 64;
}
if (smoothgain[i] == 141)
{
relais_gain_val[i] = smoothgain[i] + 64;
}
if (smoothgain[i] == 142)
{
relais_gain_val[i] = smoothgain[i] + 64;
}
if (smoothgain[i] == 143)
{
relais_gain_val[i] = smoothgain[i] + 65;
}
if (smoothgain[i] == 144)
{
relais_gain_val[i] = smoothgain[i] + 68;
}
if (smoothgain[i] == 145)
{
relais_gain_val[i] = smoothgain[i] + 70;
}
if (smoothgain[i] == 146)
{
relais_gain_val[i] = smoothgain[i] + 71;
}
if (smoothgain[i] == 147)
{
relais_gain_val[i] = smoothgain[i] + 73;
}
if (smoothgain[i] == 148)
{
relais_gain_val[i] = smoothgain[i] + 75;
}
if (smoothgain[i] == 149)
{
relais_gain_val[i] = smoothgain[i] + 78;
}
if (smoothgain[i] == 150)
{
relais_gain_val[i] = smoothgain[i] + 80;
}
if (smoothgain[i] == 151)
{
relais_gain_val[i] = smoothgain[i] + 83;
}
if (smoothgain[i] == 152)
{
relais_gain_val[i] = smoothgain[i] + 84;
}
if (smoothgain[i] == 153)
{
relais_gain_val[i] = smoothgain[i] + 87;
}
if (smoothgain[i] == 154)
{
relais_gain_val[i] = smoothgain[i] + 92;
}
if (smoothgain[i] == 155)
{
relais_gain_val[i] = smoothgain[i] + 95;
}
if (smoothgain[i] == 156)
{
relais_gain_val[i] = smoothgain[i] + 99;
}
if (i == 0)
{
relais_set(0); // mise à jour des relais
}
if (i == 1)
{
relais_set(2); // mise à jour des relais
}
}
void smoothvolume_set(int i)
{
smoothvol[i] = map(smoothvol[i], 0, 255, 0, 255); // map de 0 à 255
smoothvol[i] = constrain(smoothvol[i], 0, 255); // map de 0 à 255
relais_vol_val[i] = smoothvol[i]; // valeur du relais = valeur du volume
if (i == 0)
{
relais_set(1); // mise à jour des relais
}
if (i == 1)
{
relais_set(3); // mise à jour des relais
}
}
+100
View File
@@ -0,0 +1,100 @@
// *******************************************************************************************************
// ****************************************** PIN MAPPING ************************************************
// *******************************************************************************************************
// définition LED
#define const_out_L_led 5 // Digital OUT led const_out_L
#define const_out_R_led 6 // Digital OUT led const_out_R
#define stereo_link_led 7 // Digital OUT led stereo_link
// définition switch
#define const_out_sw_L 2 // Digital IN switch const_out_L
#define const_out_sw_R 3 // Digital IN switch const_out_R
#define stereo_link_sw 4 // Digital IN switch stereo_link
// définition PIN curseur potentiomètre
#define gain_0_pot A1 // entrée pot gain gauche
#define vol_0_pot A0 // entrée pot volume gauche
#define gain_1_pot A3 // entrée pot gain droite
#define vol_1_pot A2 // entrée pot volume droite
// *******************************************************************************************************
// ****************************************** potentiomètre **********************************************
// *******************************************************************************************************
// variables potentiomètre
int position_lue[4] = {0, 0, 0, 0}; // tableau des valeur lus sur les potentiomètre
int position_save[4] = {0, 0, 0, 0}; // tableau de sauvegarde de valeurs des potentiomètre pour comparaison
int position_set[4] = {0, 0, 0, 0}; // tableau de consigne de position des potentiomètre
int position_memory[4] = {0, 0, 0, 0}; // tableau de memorisation de valeur des potentiomètre pour mode constant output
bool position_change[4] = {false, false, false, false}; // tableau de flag de changement de position des potentiomètre
bool motor_change[4] = {false, false, false, false}; // tableau de flag pour moteurs des potentiomètre
byte analog_pot[4] = {gain_0_pot, vol_0_pot, gain_1_pot, vol_1_pot}; // tableau de PIN des potentiomètre
int max_pot[4] = {1023, 1023, 1023, 1023}; // valeur max potentiomètre pour calibrage ADC
int min_pot[4] = {0, 0, 0, 0}; // valeur min potentiomètre pour calibrage ADC
#define bounce_time_pot 200 // interval de temps entre 2 lecture de l'état du potentiomètre
// *******************************************************************************************************
// ****************************************** moteur *****************************************************
// *******************************************************************************************************
// adressage I2C carte moteur
#define gain_0_motor 0xC8 // A0 = open, A1 = open
#define vol_0_motor 0xCE // A0 = open, A1 = 1
#define gain_1_motor 0xC2 // A0 = open, A1 = 0
#define vol_1_motor 0xC6 // A0 = 0, A1 = open
int motor_speed[4] = {0, 0, 0, 0}; // tableau de valeur de vitesse des moteur
unsigned long last_motor_end[4] = {0, 0, 0, 0}; // compteur de temps d'arrêt moteur
// *******************************************************************************************************
// ****************************************** relais *****************************************************
// *******************************************************************************************************
// adressage I2C carte relais
#define gain_0_relais 0x39 // A0 = 0, A1 = 1, A2 = 1
#define vol_0_relais 0x38 // A0 = 1, A1 = 1, A2 = 1
#define gain_1_relais 0x3B // A0 = 0, A1 = 0, A2 = 1
#define vol_1_relais 0x3A // A0 = 1, A1 = 0, A2 = 1
// variables relais
byte relais_gain_val[2] = {0, 0}; // tableau de valeur des relais gain
byte relais_vol_val[2] = {0, 0}; // tableau de valeur des relais volumes
byte relais_old[4] = {0, 0, 0, 0}; // tableau de comparaison de valeur des relais
byte relais_map[4] = {0, 0, 0, 0}; // tableau de mappage des valeur des relais
#define delay_relais 1 // temps de commutation entre relais
// *******************************************************************************************************
// ****************************************** boutton ****************************************************
// *******************************************************************************************************
bool const_out_L_state_old = false; // variable de comparaison d'état du bouton const_out_L
bool const_out_R_state_old = false; // variable de comparaison d'état du bouton const_out_R
bool stereo_link_state_old = false; // variable de comparaison d'état du bouton stereo_link
bool const_out_L_state = false; // variable d'état du bouton const_out_L
bool const_out_R_state = false; // variable d'état du bouton const_out_R
bool stereo_link_state = false; // variable d'état du bouton stereo_link
bool state_button_change = false; // variable de changement d'état des boutons
#define interval_button 10 // interval de temps entre 2 lecture de l'état du bouton
// *******************************************************************************************************
// ****************************************** variable diverse *******************************************
// *******************************************************************************************************
int smoothgain[2] = {0, 0}; // tableau de valeur de lissage du gain
int smoothvol[2] = {0, 0}; // tableau de valeur de lissage du volume
// bool const_state = false; // variable d'état constant ouptut/stereo_link
// bool error_motor = false; // variable d'erreur de communication avec le moteur
// *******************************************************************************************************
// ****************************************** fonction erreur LED ***************************************
// *******************************************************************************************************
bool error_state = false;
bool error_led_state = LOW;
unsigned long old_led_time = millis(); // variable de temps courant
void error_led()
{
unsigned long current_led_time = millis(); // variable de temps courant
if (current_led_time - old_led_time >= 100)
{
old_led_time = millis();
error_led_state = !error_led_state;
digitalWrite(stereo_link_led, error_led_state);
}
}