Ciao a tutti e buon ferragosto.
Come accennato nel titolo ho un problema con il comando di servo ed esc utilizzando le suddette librerie.
Video del problema
Dico subito che ho provato ad utilizzare esclusivamente l'invio dei comandi, omettendo dal codice tutto quello che riguarda l'osd e la IMU e il servo e gli esc funzionano benissimo.
Qualcuno potrebbe fare un'ipotesi sul motivo del problema? Altrimenti dovrò utilizzare un arduino per ricevere i comandi e comandare i motori ed il servo e un'altro arduino per la lettura dei sensori e dei valori dell'IMU e scriverli a schermo.
Aggiungo che utilizzo 2 schedine con RS485 per la comunicazione.
Software della base:
#include <EasyTransfer.h>
EasyTransfer ETout;
struct SEND_DATA_STRUCTURE{
byte fw_Motor_Sx_command;
byte fw_Motor_Dx_command;
byte up_Motor_Sx_command;
byte up_Motor_Dx_command;
byte tilt_Camera_command;
byte on_off_led;
};
SEND_DATA_STRUCTURE txdata;
#define FWD_REW A4
#define RIGHT_LEFT A3
#define UP_DOWN A1
#define BUTTON_UP_CAMERA A0
#define BUTTON_DOWN_CAMERA A2
#define BUTTON_ON_OFF_LIGHT A5
static const float JS_CENTER_0_RANGE = 50;
static const float JS_CENTER_1_RANGE = 50;
static const float JS_CENTER_2_RANGE = 50;
static const int JS_CENTER_0 = 512;
static const int JS_CENTER_1 = 512;
static const int JS_CENTER_2 = 512;
int Js_Center_0_Range = 0;
int Js_Center_0 = 0;
int Js_Center_1_Range = 0;
int Js_Center_1 = 0;
int Js_Center_2_Range = 0;
int Js_Center_2 = 0;
static const int JS_RANGE_0 = 1023;
static const int JS_RANGE_1 = 1023;
static const int JS_RANGE_2 = 1023;
static const int JS_DIR_0 = 1; // +1 or -1
static const int JS_DIR_1 = 1;
static const int JS_DIR_2 = 1;
int thrusterLeftCommand;
int thrusterRightCommand;
int cameraCommand = 128;
int ledState = LOW;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void mapJoy() {
Js_Center_0_Range = ((JS_CENTER_0_RANGE / JS_RANGE_0) * 200);
Js_Center_0 = map(JS_CENTER_0,
0,
JS_RANGE_0,
-100,
100);
Js_Center_1_Range = ((JS_CENTER_1_RANGE / JS_RANGE_1) * 200);
Js_Center_1 = map(JS_CENTER_1,
0,
JS_RANGE_1,
-100,
100);
Js_Center_2_Range = ((JS_CENTER_2_RANGE / JS_RANGE_2) * 200);
Js_Center_2 = map(JS_CENTER_2,
0,
JS_RANGE_2,
-100,
100);
}
void setup() {
Serial.begin(19200);
ETout.begin(details(txdata), &Serial);
mapJoy();
delay(1000);
}
void loop() {
int reading = digitalRead(BUTTON_ON_OFF_LIGHT);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
lastButtonState = reading;
int camera_up = analogRead(BUTTON_UP_CAMERA);
if (camera_up > 900) cameraCommand ++;
if (cameraCommand > 220) cameraCommand = 220;
int camera_down = analogRead(BUTTON_DOWN_CAMERA);
if (camera_down > 900) cameraCommand --;
if (cameraCommand < 35) cameraCommand = 35;
int forwardCommand = analogRead(FWD_REW);
int turnCommand = analogRead(RIGHT_LEFT);
int verticalCommand = analogRead(UP_DOWN);
forwardCommand = map(forwardCommand,
0,
JS_RANGE_0, // Joystick high value
100, // Command low value
-100); // Command high value
turnCommand = map(turnCommand, // raw joystick value
0, // Joystick low value
JS_RANGE_1, // Joystick high value
-100, // Command low value
100); // Command high value
verticalCommand = map(verticalCommand, // raw joystick value
0, // Joystick low value
JS_RANGE_2, // Joystick high value
-100, // Command low value
100); // Command high value
if ((turnCommand > Js_Center_1 - Js_Center_1_Range) &&
(turnCommand < Js_Center_1 + Js_Center_1_Range))
{
if ((forwardCommand > Js_Center_0 - Js_Center_0_Range) &&
(forwardCommand < Js_Center_0 + Js_Center_0_Range))
{
thrusterLeftCommand = Js_Center_0;
thrusterRightCommand = Js_Center_0;
}
else
{
thrusterLeftCommand = forwardCommand - Js_Center_0_Range;
thrusterRightCommand = forwardCommand - Js_Center_0_Range;
}
}
else if ((forwardCommand > Js_Center_0 - Js_Center_0_Range) &&
(forwardCommand < Js_Center_0 + Js_Center_0_Range))
{
if ((turnCommand > Js_Center_1 - Js_Center_1_Range) &&
(turnCommand < Js_Center_1 + Js_Center_1_Range))
{
thrusterLeftCommand = Js_Center_0;
thrusterRightCommand = Js_Center_0;
}
else
{
thrusterLeftCommand = - turnCommand + Js_Center_1_Range;
thrusterRightCommand = turnCommand - Js_Center_1_Range;
}
}
else if (forwardCommand > Js_Center_0 + Js_Center_0_Range)
{
thrusterLeftCommand = forwardCommand - turnCommand;
thrusterRightCommand = forwardCommand + turnCommand;
}
else if (forwardCommand < Js_Center_0 - Js_Center_0_Range)
{
thrusterLeftCommand = forwardCommand + turnCommand;
thrusterRightCommand = forwardCommand - turnCommand;
}
if (thrusterLeftCommand > 100) thrusterLeftCommand = 100;
else if (thrusterLeftCommand < -100) thrusterLeftCommand = -100;
if (thrusterRightCommand > 100) thrusterRightCommand = 100;
else if (thrusterRightCommand < -100) thrusterRightCommand = -100;
txdata.fw_Motor_Sx_command = map(thrusterLeftCommand, -100, 100, 0, 255);
txdata.fw_Motor_Dx_command = map(thrusterRightCommand, -100, 100, 0, 255);
txdata.up_Motor_Sx_command = map(verticalCommand, 100, -100, 0, 255);
txdata.up_Motor_Dx_command = map(verticalCommand, 100, -100, 0, 255);
txdata.tilt_Camera_command = cameraCommand;
txdata.on_off_led = ledState;
ETout.sendData();
}
Aggiungo un alto post perchè non ci va tutto!!