Commande de 8 fonctions par script Lua pour OpenTx/EdgeTx

pierrotm777

Membre Sénior
Bonjour à tous,

Voici un nouveau projet, toujours au tour de la télémétrie.
Ce projet est basé un une autre bibliothèque Arduino qui gère le protocole S-Port de Frsky.
On trouve cette bibliothèque ici.
Les radio basées sur OpenTx ou EdgeTx peuvent utiliser des scripts Lua qui permettent de lire des données ou de configurer différentes choses, soit dans la radio, soit dans le récepteur.
Depuis pas mal d'année maintenant, le groupe OpenAVRc dont je fais partie a développé un système du même type qu'OpenTx.
On y a intégré un système que l'on a appelé X-Any qui permet d'utiliser les anciens modules Multiswitch de Futaba, Multiplex ou Graupner normalement non utilisables en 2,4Ghz, mais aussi nos cartes Ms8 et Ms16 qui permettent énormément plus d'options que ces anciens modules.
Hors, ajouter X-Any dans les radios OpenTx/EdgeTx n'est pas prévu bien que ce serait possible.
A la découverte de cette nouvelle librairie, je me suis rendu compte qu'en plus de retourner les données de différents capteurs, gps, baromètre, courant, tension, rpm etc... , on pouvait lancer des commandes vers le récepteur grâce à un script Lua. Lua est un language script.
Après pas mal de tests, j'ai donc créé un module Arduino Pro Mini qui commande 8 sorties tout ou rien pour commander ce que l'on veut.
Ce module basé sur un Arduino Pro Mini est commandé par un script Lua dédié.
Mais comme j'avais sous la main nos prototypes Ms8(Attiny84) et Ms16(Pro Micro), j'ai aussi tenté d'utiliser ces cartes en reprogramment l'attiny84 du Ms8 et le Pro Micro du Ms16.
Pour l'instant, ça ne fonctionne pas mais on ne désespère pas.

A suivre...
 
Bonjour à tous,

J'ai réussi à faire fonctionner mon système de 8 commandes via un script Lua.
Le script Lua doit être placé sur la carte SD:
SCRIPTS/TOOLS/

Voici le script Lua:
Code:
local command1 = 0
local command2 = 0
local command3 = 0
local command4 = 0
local command5 = 0
local command6 = 0
local command7 = 0
local command8 = 0

local alarm1 = 0
local alarm2 = 0
local selected = 0
local commanddisplay = {}

local function loadBrandBack()
    back = Bitmap.open("8COMMANDS_img/back1.png")
end

local function init()
      for i=0,31,1 do
        commanddisplay[i] = '#'
    end
  
    loadBrandBack()
  
    --request config data from sensor
      sportTelemetryPush(0x1B, 0x30, 0x5200, 0x01)
end

local function loadWaitBack()
    --back = Bitmap.open("/scripts/tools/8COMMANDS_img/back_ecu.png")
end


local function loadBrandBack()
    back = Bitmap.open("/SCRIPTS/TOOLS/8COMMANDS_img/back1.png")
end

local function run(event)
  
    if event == nil then
        error("Cannot be run as a model script!")
        return 2
    elseif event == EVT_EXIT_BREAK then
        return 2
    elseif event == EVT_MODEL_FIRST then
        --todo
    elseif event == EVT_ENTER_BREAK or event == EVT_SYS_FIRST then
        --todo
    elseif event == EVT_ENTER_LONG or event == EVT_TELEM_FIRST then
        if selected == 0 then
            sportTelemetryPush(0x1B, 0x31, 0x5200, command1)
        elseif selected == 1 then
            sportTelemetryPush(0x1B, 0x31, 0x5201, command2)
        elseif selected == 2 then
            sportTelemetryPush(0x1B, 0x31, 0x5202, command3)
        elseif selected == 3 then
            sportTelemetryPush(0x1B, 0x31, 0x5203, command4)
        elseif selected == 4 then
            sportTelemetryPush(0x1B, 0x31, 0x5204, command5)
        elseif selected == 5 then
            sportTelemetryPush(0x1B, 0x31, 0x5205, command6)
        elseif selected == 6 then
            sportTelemetryPush(0x1B, 0x31, 0x5206, command7)
        elseif selected == 7 then
            sportTelemetryPush(0x1B, 0x31, 0x5207, command8)
        end
    elseif event == EVT_PAGE_BREAK or event == EVT_PAGEDN_FIRST  then
        if selected == 0 then
            selected = 1
        elseif selected == 1 then
            selected = 2
        elseif selected == 2 then
            selected = 3
        elseif selected == 3 then
            selected = 4
        elseif selected == 4 then
            selected = 5
        elseif selected == 5 then
            selected = 6
        elseif selected == 6 then
            selected = 7
        end
    elseif event == EVT_PAGE_LONG or event == EVT_PAGEUP_FIRST then
        if selected == 7 then
            selected = 6
        elseif selected == 6 then
            selected = 5
        elseif selected == 5 then
            selected = 4
        elseif selected == 4 then
            selected = 3
        elseif selected == 3 then
            selected = 2
        elseif selected == 2 then
            selected = 1
        elseif selected == 1 then
            selected = 0
        end
    elseif event == EVT_PLUS_FIRST or event == EVT_ROT_RIGHT or event == EVT_PLUS_REPT then
      if selected == 0 then
        command1 = 1
      elseif selected == 1 then
        command2 = 1
      elseif selected == 2 then
        command3 = 1
      elseif selected == 3 then
        command4 = 1
      elseif selected == 4 then
        command5 = 1
      elseif selected == 5 then
        command6 = 1
      elseif selected == 6 then
        command7 = 1
      elseif selected == 7 then
        command8 = 1
      end
    elseif event == EVT_MINUS_FIRST or event == EVT_ROT_LEFT or event == EVT_MINUS_REPT then
      if selected == 0 then
        command1 = 0
      elseif selected == 1 then
        command2 = 0
      elseif selected == 2 then
        command3 = 0
      elseif selected == 3 then
        command4 = 0
      elseif selected == 4 then
        command5 = 0
      elseif selected == 5 then
        command6 = 0
      elseif selected == 6 then
        command7 = 0
      elseif selected == 7 then
        command8 = 0
      end
    end

    local physicalId, primId, dataId, value = sportTelemetryPop()
  
    while physicalId ~= nil do
        if dataId == 0x5200 then
            command1 = value
              sportTelemetryPush(0x1B, 0x30, 0x5201, 0x02)

        elseif dataId == 0x5201 then
            command2 = value
        elseif dataId == 0x5202 then
            command3 = value
        elseif dataId == 0x5203 then
            command4 = value
        elseif dataId == 0x5204 then
            command5 = value
        elseif dataId == 0x5205 then
            command6 = value
        elseif dataId == 0x5206 then
            command7 = value
        elseif dataId == 0x5207 then
            command8 = value 
        end

        physicalId, primId, dataId, value = sportTelemetryPop()
    end
  
    lcd.clear()
    lcd.drawFilledRectangle(0, 0, LCD_W, LCD_H, TEXT_BGCOLOR)
    lcd.drawFilledRectangle(0, 0, LCD_W, 30, TITLE_BGCOLOR)
    lcd.drawText(1, 5, "Command Sensor Config", MENU_TITLE_COLOR)
  
    lcd.drawText(1, 30, "command1: ", TEXT_COLOR)
    if selected == 0 then
        if command1 == 1 then
            lcd.drawText(100, 30, "ON", LEFT + TEXT_COLOR + INVERS)
        else
            lcd.drawText(100, 30, "OFF", LEFT + TEXT_COLOR + INVERS)
        end
    else
        if command1 == 1 then
            lcd.drawText(100, 30, "ON", LEFT + TEXT_COLOR)
        else
            lcd.drawText(100, 30, "OFF", LEFT + TEXT_COLOR)
        end     
    end
  
    lcd.drawText(1, 55, "command2: ", TEXT_COLOR)
    if selected == 1 then
        if command2 == 1 then
            lcd.drawText(100, 55, "ON", LEFT + TEXT_COLOR + INVERS)
        else
            lcd.drawText(100, 55, "OFF", LEFT + TEXT_COLOR + INVERS)
        end     
    else
        if command2 == 1 then
            lcd.drawText(100, 55, "ON", LEFT + TEXT_COLOR)
        else
            lcd.drawText(100, 55, "OFF", LEFT + TEXT_COLOR)
        end         
    end
  
    lcd.drawText(1, 80, "command3: ", TEXT_COLOR)
    if selected == 2 then
        if command3 == 1 then
            lcd.drawText(100, 80, "ON", LEFT + TEXT_COLOR + INVERS)
        else
            lcd.drawText(100, 80, "OFF", LEFT + TEXT_COLOR + INVERS)
        end     
    else
        if command3 == 1 then
            lcd.drawText(100, 80, "ON", LEFT + TEXT_COLOR)
        else
            lcd.drawText(100, 80, "OFF", LEFT + TEXT_COLOR)
        end         
    end 
  
    -- -------------------------------------
  
    lcd.drawText(1, 105, "command4: ", TEXT_COLOR)
    if selected == 3 then
        if command4 == 1 then
            lcd.drawText(100, 105, "ON", LEFT + TEXT_COLOR + INVERS)
        else
            lcd.drawText(100, 105, "OFF", LEFT + TEXT_COLOR + INVERS)
        end     
    else
        if command4 == 1 then
            lcd.drawText(100, 105, "ON", LEFT + TEXT_COLOR)
        else
            lcd.drawText(100, 105, "OFF", LEFT + TEXT_COLOR)
        end         
    end 
  
    lcd.drawText(1, 130, "command5: ", TEXT_COLOR)
    if selected == 4 then
        if command5 == 1 then
            lcd.drawText(100, 130, "ON", LEFT + TEXT_COLOR + INVERS)
        else
            lcd.drawText(100, 130, "OFF", LEFT + TEXT_COLOR + INVERS)
        end     
    else
        if command5 == 1 then
            lcd.drawText(100, 130, "ON", LEFT + TEXT_COLOR)
        else
            lcd.drawText(100, 130, "OFF", LEFT + TEXT_COLOR)
        end         
    end 
  
    lcd.drawText(1, 155, "command6: ", TEXT_COLOR)
    if selected == 5 then
        if command6 == 1 then
            lcd.drawText(100, 155, "ON", LEFT + TEXT_COLOR + INVERS)
        else
            lcd.drawText(100, 155, "OFF", LEFT + TEXT_COLOR + INVERS)
        end     
    else
        if command6 == 1 then
            lcd.drawText(100, 155, "ON", LEFT + TEXT_COLOR)
        else
            lcd.drawText(100, 155, "OFF", LEFT + TEXT_COLOR)
        end         
    end 
  
    lcd.drawText(1, 180, "command7: ", TEXT_COLOR)
    if selected == 6 then
        if command7 == 1 then
            lcd.drawText(100, 180, "ON", LEFT + TEXT_COLOR + INVERS)
        else
            lcd.drawText(100, 180, "OFF", LEFT + TEXT_COLOR + INVERS)
        end     
    else
        if command7 == 1 then
            lcd.drawText(100, 180, "ON", LEFT + TEXT_COLOR)
        else
            lcd.drawText(100, 180, "OFF", LEFT + TEXT_COLOR)
        end         
    end 

    lcd.drawText(1, 205, "command8: ", TEXT_COLOR)
    if selected == 7 then
        if command8 == 1 then
            lcd.drawText(100, 205, "ON", LEFT + TEXT_COLOR + INVERS)
        else
            lcd.drawText(100, 205, "OFF", LEFT + TEXT_COLOR + INVERS)
        end     
    else
        if command8 == 1 then
            lcd.drawText(100, 205, "ON", LEFT + TEXT_COLOR)
        else
            lcd.drawText(100, 205, "OFF", LEFT + TEXT_COLOR)
        end         
    end 
    lcd.drawText(1, 240, "Use TELE to update the selected value ", TEXT_COLOR)
  
    lcd.setColor(CUSTOM_COLOR, WHITE)
    lcd.drawText(170, 103, "Command " .. selected + 1 .. " ready !", CUSTOM_COLOR + DBLSIZE)
    lcd.setColor(CUSTOM_COLOR, BLACK)
    lcd.drawText(173, 100, "Command " .. selected + 1 .. " ready !", CUSTOM_COLOR + DBLSIZE)
  
    return 0
end

return { init=init, run=run }
 

Fichiers joints

  • SCRIPTS.zip
    117,6 KB · Vues: 3
Voici le code Arduino correspondant:
C++:
#include <SPort.h>
#include <EEPROM.h>

//#define SPORT_PIN 8//Attiny84
#define SPORT_PIN 16//Pro Mini/Digispark Pro
//#define SPORT_PIN Serial1//Pro Micro

#define SPORT_PHYSICAL_ID 0x12
#define SPORT_COMMAND_ID 0x1B

#define SENSOR_CONF_COMMAND1  0x5200
#define SENSOR_CONF_COMMAND2  0x5201
#define SENSOR_CONF_COMMAND3  0x5202
#define SENSOR_CONF_COMMAND4  0x5203
#define SENSOR_CONF_COMMAND5  0x5204
#define SENSOR_CONF_COMMAND6  0x5205
#define SENSOR_CONF_COMMAND7  0x5206
#define SENSOR_CONF_COMMAND8  0x5207


SPortHub hub(SPORT_PHYSICAL_ID, SPORT_PIN);
SimpleSPortSensor command1(SENSOR_CONF_COMMAND1);
SimpleSPortSensor command2(SENSOR_CONF_COMMAND2);
SimpleSPortSensor command3(SENSOR_CONF_COMMAND3);
SimpleSPortSensor command4(SENSOR_CONF_COMMAND4);
SimpleSPortSensor command5(SENSOR_CONF_COMMAND5);
SimpleSPortSensor command6(SENSOR_CONF_COMMAND6);
SimpleSPortSensor command7(SENSOR_CONF_COMMAND7);
SimpleSPortSensor command8(SENSOR_CONF_COMMAND8);

uint8_t command1Value = 0;
uint8_t command2Value = 0;
uint8_t command3Value = 0;
uint8_t command4Value = 0;
uint8_t command5Value = 0;
uint8_t command6Value = 0;
uint8_t command7Value = 0;
uint8_t command8Value = 0;


byte outputPins[] = {3,4,5,6,7,8,9,10};//Pro Mini
byte pinOutCount = sizeof(outputPins);

void setup() {
  //Serial.end();
  for (byte i = 0; i < pinOutCount; i++) {// Set the selected pins as an output and set the pullup resistor
    pinMode(outputPins[i],OUTPUT);
    digitalWrite(outputPins[i],LOW);
    delay(100);
    digitalWrite(outputPins[i],HIGH);
  }

  hub.registerSensor(command1);
  hub.registerSensor(command2);
  hub.registerSensor(command3);
  hub.registerSensor(command4);
  hub.registerSensor(command5);
  hub.registerSensor(command6);
  hub.registerSensor(command7);
  hub.registerSensor(command8);
 
  hub.commandId = SPORT_COMMAND_ID;
  hub.commandReceived = commandReceived;
  hub.begin();

  pinMode(17, OUTPUT);

}


void loop() {
  hub.handle();
  command1.value = (uint16_t)command1Value;
  command2.value = (uint16_t)command2Value;
  command3.value = (uint16_t)command3Value;
  command4.value = (uint16_t)command4Value;
  command5.value = (uint16_t)command5Value;
  command6.value = (uint16_t)command6Value;
  command7.value = (uint16_t)command7Value;
  command8.value = (uint16_t)command8Value;

}

void commandReceived(int prim, int applicationId, int value) {
  if (prim == SPORT_HEADER_READ) { //Read
    if (applicationId == SENSOR_CONF_COMMAND1)
    {
        hub.sendCommand(SPORT_HEADER_RESPONSE, applicationId, command1Value);
    } else if(applicationId == SENSOR_CONF_COMMAND2) {
        hub.sendCommand(SPORT_HEADER_RESPONSE, applicationId, command2Value);
    } else if(applicationId == SENSOR_CONF_COMMAND3) {
        hub.sendCommand(SPORT_HEADER_RESPONSE, applicationId, command3Value);
    }  else if(applicationId == SENSOR_CONF_COMMAND4) {
        hub.sendCommand(SPORT_HEADER_RESPONSE, applicationId, command4Value);
    } else if(applicationId == SENSOR_CONF_COMMAND5) {
        hub.sendCommand(SPORT_HEADER_RESPONSE, applicationId, command5Value);
    } else if(applicationId == SENSOR_CONF_COMMAND6) {
        hub.sendCommand(SPORT_HEADER_RESPONSE, applicationId, command6Value);
    } else if(applicationId == SENSOR_CONF_COMMAND7) {
        hub.sendCommand(SPORT_HEADER_RESPONSE, applicationId, command7Value);
    } else if(applicationId == SENSOR_CONF_COMMAND8) {
        hub.sendCommand(SPORT_HEADER_RESPONSE, applicationId, command8Value);
    }
  } else if(prim == SPORT_HEADER_WRITE) { //Write
    if (applicationId == SENSOR_CONF_COMMAND1) {
      command1Value = value;
      //saveData();
    } else if(applicationId == SENSOR_CONF_COMMAND2) {
      command2Value = value;
      //saveData();
    } else if(applicationId == SENSOR_CONF_COMMAND3) {
      command3Value = value;
      //saveData();
    } else if(applicationId == SENSOR_CONF_COMMAND4) {
      command4Value = value;
      //saveData();
    } else if(applicationId == SENSOR_CONF_COMMAND5) {
      command5Value = value;
      //saveData();
    } else if(applicationId == SENSOR_CONF_COMMAND6) {
      command6Value = value;
      //saveData();
    } else if(applicationId == SENSOR_CONF_COMMAND7) {
      command7Value = value;
      //saveData();
    } else if(applicationId == SENSOR_CONF_COMMAND8) {
      command8Value = value;
      //saveData();
    }
  }
  saveData();
}

void saveData() {
  EEPROM.write(0, command1Value);(command1Value==1?digitalWrite(outputPins[0],LOW):digitalWrite(outputPins[0],HIGH));
  EEPROM.write(1, command2Value);(command2Value==1?digitalWrite(outputPins[1],LOW):digitalWrite(outputPins[1],HIGH));
  EEPROM.write(2, command3Value);(command3Value==1?digitalWrite(outputPins[2],LOW):digitalWrite(outputPins[2],HIGH));
  EEPROM.write(3, command4Value);(command4Value==1?digitalWrite(outputPins[3],LOW):digitalWrite(outputPins[3],HIGH));
  EEPROM.write(4, command5Value);(command5Value==1?digitalWrite(outputPins[4],LOW):digitalWrite(outputPins[4],HIGH));
  EEPROM.write(5, command6Value);(command6Value==1?digitalWrite(outputPins[5],LOW):digitalWrite(outputPins[5],HIGH));
  EEPROM.write(6, command7Value);(command7Value==1?digitalWrite(outputPins[6],LOW):digitalWrite(outputPins[6],HIGH));
  EEPROM.write(7, command8Value);(command8Value==1?digitalWrite(outputPins[7],LOW):digitalWrite(outputPins[7],HIGH));
  digitalWrite(17, HIGH);
  delay(200);
  digitalWrite(17, LOW);
}

void loadData() {
  command1Value = EEPROM.read(0);
  command2Value = EEPROM.read(1);
  command3Value = EEPROM.read(2);
  command4Value = EEPROM.read(3);
  command5Value = EEPROM.read(4);
  command6Value = EEPROM.read(5);
  command7Value = EEPROM.read(6);
  command8Value = EEPROM.read(7);
  digitalWrite(17, LOW);
  delay(200);
  digitalWrite(17, HIGH);
}
 
Voici le code Arduino correspondant:
C++:
Bonjour
j'ai aussi essayé de faire qq chose avec le smartport
j'arrive a faire passer 32 fonctions tout ou rien grâce aux smartport
j'ai encore des bug sur le widget je n'arrive pas a faire fonctionner ensemble deux configuration différente, mais en théorie ça devrait marcher a un moment
le code arduino sans bibliothèque ( marche et tester sur esp32), on peut recevoir les 4 octets de données

C++:
uint8_t SportTxValue[4] = { 0, 0, 0, 0 };
uint8_t SportValue[4] = { 0, 0, 0, 0 };
uint8_t dataArray[10] = { 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,0 };


//uint8_t adresse =  0x0D;
uint8_t dataCounter = 0;
bool complet = false;
bool valide = false;
byte data =0;

void setup() {
pinMode(39, INPUT_PULLDOWN);
Serial.begin(115200);
Serial2.begin(57600,SERIAL_8N1,39,36,false);

}

void loop() {
 
 SportRead(0x0D);
 
  static unsigned long printSbusMillis;

  if (millis() - printSbusMillis > 300) // Every 1000ms
  {
    printSbusMillis = millis();

 
 
 
    Serial.println("SportValue[0]");
    Serial.println(SportValue[0],BIN);
    Serial.println("SportValue[1]");
    Serial.println(SportValue[1],BIN);
    Serial.println("SportValue[2]");
    Serial.println(SportValue[2],BIN);
    Serial.println("SportValue[3]");
    Serial.println(SportValue[3],BIN);
 
}





}
void SportRead(uint8_t adresse)
{
 
 while (Serial2.available()>0) {
      data = Serial2.read();
 
    if (data == 0x7E) {
        dataCounter = 0;
        complet = false;
        valide =true;
      }

    dataArray[dataCounter] = data;
      dataCounter++;
   
    if ((dataCounter == 10) && (dataArray[1] == adresse)) {
        SportTxValue[0] = dataArray[5];
        SportTxValue[1] = dataArray[6];
        SportTxValue[2] = dataArray[7];
        SportTxValue[3] = dataArray[8];
   
       complet = true;
       }
 

 }

    if (complet && ((SportValue[0] != SportTxValue[0])|| (SportValue[1] != SportTxValue[1])|| (SportValue[2] != SportTxValue[2])|| (SportValue[3] != SportTxValue[3])))
    {
    SportValue[0] = SportTxValue[0];
    SportValue[1] = SportTxValue[1];
    SportValue[2] = SportTxValue[2];
    SportValue[3] = SportTxValue[3];

    complet = false;
 
  }
}

 

Fichiers joints

  • TSwitchSport.zip
    20,5 KB · Vues: 1
Dernière édition:
Salut,
Content de voir que quelqu'un s'intéresse à l'idée.
Tu utilises un ESP32. Mais ça devrait aussi fonctionner sur un Uno ou Pro Mini je suppose.
Je test ça rapidement.
 
Haut