Code du projet PIGOU

Code utilisé pour le projet PIGOU

PIGOU - Java

Le projet JAVA ainsi que les code ci dessous on été versionné et disponible librement

Direction GitHub

Arduino

#include  
#include 

//constante des leds
const int led[] = {32,31,30,29,28,27,26,25,24,23,22,2,4,5};
const int nbLed = 14;
//donnée des clignotement pour les leds
int ledBlink[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//constante des inputs
const int inputButton[] = {35,36,37,38,39,40,41,42,43,44,45};
const int inputSwitch[] = {A0,34,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,33,A14,A15,53,52,51,50,49,48,47,46};
const int nbSwitch = 24;
const int nbButton = 11;
//constante des variables d'états des entrées
//etat relaché par défaut LOW
int currentStateInputSwitch[] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
int currentStateInputButton[] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
int finalStateInputButton[] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};

int currentStateRead;
int currentPort;

//donne entrant du port série
String inputSerial = "";
//initialisation de l'écran
LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  
  //mise en place de l'écran
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0,0);
  
  //ecoute du port série
  Serial.begin(9600);

  //initialisation des leds et leur mode
  for (int i = 0; i < nbLed; i++) {
    pinMode(led[i], OUTPUT);
    digitalWrite(led[i], HIGH);
  }
  
  //initialisation des entrées et leur mode
  for (int i = 0; i < nbButton; i++) {
    pinMode(inputButton[i], INPUT_PULLUP);
  }
  for (int i = 0; i < nbSwitch; i++) {
    pinMode(inputSwitch[i], INPUT_PULLUP);
  }

  //etalonnage des etats des boutons actuelles
  for (int i = 0; i < nbButton; i++) {
    currentStateRead = digitalRead(inputButton[i]);
    currentStateInputButton[i] = currentStateRead;
    finalStateInputButton[i] = currentStateRead;
  }
  Serial.println();
}

void loop() {
  for (int i = 0; i < nbLed; i++) {
    if (ledBlink[i] == 1)
      digitalWrite(led[i], LOW);
  }
  delay(200);
  for (int i = 0; i < nbLed; i++) {
    if (ledBlink[i] == 1)
      digitalWrite(led[i], HIGH);
  }
  delay(200);
  
  //lecture et interpretation pour les interrupteurs
  for (int i = 0; i < nbSwitch; i++) {
    currentStateRead = digitalRead(inputSwitch[i]);
    if (currentStateRead != currentStateInputSwitch[i]) {
      String output = String(i);
      if (i < 10) {
        output = "0" + output;
      }
      Serial.print(output);
      currentStateInputSwitch[i] = currentStateRead;
    }
  }

  currentPort = nbSwitch;
  //lecture et interprétation des boutons
  for (int i = 0; i < nbButton; i++) {
    currentStateRead = digitalRead(inputButton[i]);
    if (currentStateRead != currentStateInputButton[i] && finalStateInputButton[i] != currentStateRead) {
      String output = String(currentPort);
      if (currentPort < 10) {
        output = "0" + output;
      }
      Serial.print(output);
    }
    currentStateInputButton[i] = currentStateRead;
    currentPort++;
  }
}

void serialEvent() {
  //récupération des données
  inputSerial = "";
  while (Serial.available() > 0)
  {
    inputSerial = inputSerial + ascii(Serial.read());
  }

  //lecture de la donnée entrante et allumage des LED
  int i;
  for (i = 0; i < nbLed; i++) {
    if (inputSerial.substring(i, i+1) == "0") {
      digitalWrite(led[i], HIGH);
      ledBlink[i] = 0;
    } else if (inputSerial.substring(i, i+1) == "1") {
      digitalWrite(led[i], LOW);
      ledBlink[i] = 0;
    } else if (inputSerial.substring(i, i+1) == "2") {
      ledBlink[i] = 1;
    }
  }
  //gestion de l'écran
  setLcd(inputSerial.substring(i, i+1), inputSerial.substring(i+1, i+4), inputSerial.substring(i+4, i+7));
}

/**
 * fonction de changement d'état de l'écran LCD 
 * partie gestion de l'oxygen et de la température du liquide de refroidissement
 */
void setLcd(String arg_options, String arg_num1, String arg_num2) {
  //réinitialisation de l'écran
  lcd.setCursor(0,0);
  lcd.print("                ");
  lcd.setCursor(0,1);
  lcd.print("                ");  
  lcd.setCursor(0,0);
  
  //message selon option si option 0 aucun affichage
  if ( arg_options == "1" ) {
    lcd.print("No connection");
    lcd.setCursor(0,1);
    lcd.print("on APU");
  } 
  if ( arg_options == "2" ) {
    lcd.print("!!Warning!!");
    lcd.setCursor(0,1);
    lcd.print("O2/N2 low");
  }
  if (arg_options == "3") {
    lcd.print("!!Warning!!");
    lcd.setCursor(0,1);
    lcd.print("hoverheated");
  }
  if ( arg_options == "4") {
    lcd.print("O2/N2 : ");
    lcd.setCursor(0,1);
    lcd.print("System T : ");
    lcd.setCursor(9,0);
    lcd.print(arg_num1 + " %");
    lcd.setCursor(9,1);
    lcd.print(arg_num2 + " C");
  }   
}

/**
 * fonction de traduction des données entrant en asciis
 */
String ascii(int arg_dec) {
  if (arg_dec == 48) return "0";
  if (arg_dec == 49) return "1";
  if (arg_dec == 50) return "2";
  if (arg_dec == 51) return "3";
  if (arg_dec == 52) return "4";
  if (arg_dec == 53) return "5";
  if (arg_dec == 54) return "6";
  if (arg_dec == 55) return "7";
  if (arg_dec == 56) return "8";
  if (arg_dec == 57) return "9";
}
#include  
#include 

//constante des leds
const int led[] = {A0,A1,A2,2,3,4,5,6,7,8,9,10,11};
const int nbLed = 13;
//donnée des clignotement pour les leds
int ledBlink[] = {0,0,0,0,0,0,0,0,0,0,0,0,0};

//donne entrant du port série
String inputSerial = "";
//initialisation de l'écran
LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  //mise en place de l'écran
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0,0);

  //ecoute du port série
  Serial.begin(9600);

  //initialisation des leds et leur mode
  for (int i = 0; i < nbLed; i++) {
    pinMode(led[i], OUTPUT);
    digitalWrite(led[i], HIGH);
  }
}

void loop() {
  for (int i = 0; i < nbLed; i++) {
    if (ledBlink[i] == 1)
      digitalWrite(led[i], LOW);
  }
  delay(200);
  for (int i = 0; i < nbLed; i++) {
    if (ledBlink[i] == 1)
      digitalWrite(led[i], HIGH);
  }
  delay(200);
}

void serialEvent() {
  //récupération des données
  inputSerial = "";
  while (Serial.available() > 0)
  {
    inputSerial = inputSerial + ascii(Serial.read());
  }

  //lecture de la donnée entrante et allumage des LED
  int i;
  for (i = 0; i < nbLed; i++) {
    if (inputSerial.substring(i, i+1) == "0") {
      digitalWrite(led[i], HIGH);
      ledBlink[i] = 0;
    } else if (inputSerial.substring(i, i+1) == "1") {
      digitalWrite(led[i], LOW);
      ledBlink[i] = 0;
    } else if (inputSerial.substring(i, i+1) == "2") {
      ledBlink[i] = 1;
    }
  }
  //gestion de l'écran
  setLcd(inputSerial.substring(i, i+1), inputSerial.substring(i+1, i+5), inputSerial.substring(i+5, i+8));
}

/**
 * fonction de changement d'état de l'écran LCD 
 * partie gestion de l'oxygen et de la température du liquide de refroidissement
 */
void setLcd(String arg_options, String arg_num1, String arg_num2) {
  //réinitialisation de l'écran
  lcd.setCursor(0,0);
  lcd.print("                ");
  lcd.setCursor(0,1);
  lcd.print("                ");  
  lcd.setCursor(0,0);
  
  //message selon option si option 0 aucun affichage
  if ( arg_options == "1" ) {
    lcd.print("No connection");
    lcd.setCursor(0,1);
    lcd.print("on APU");
  } 
  if ( arg_options == "2" ) {
    lcd.print("!!Warning!!");
    lcd.setCursor(0,1);
    lcd.print("Main low");
  }
  if ( arg_options == "3" ) {
    lcd.print("!!Warning!!");
    lcd.setCursor(0,1);
    lcd.print("RCS low");
  }
  if ( arg_options == "4" ) {
    lcd.print("Main : ");
    lcd.setCursor(0,1);
    lcd.print("RCS : ");
    lcd.setCursor(8,0);
    lcd.print(arg_num1 + " L");
    lcd.setCursor(10,1);
    lcd.print(arg_num2 + " L");
  }   
}

/**
 * fonction de traduction des données entrant en asciis
 */
String ascii(int arg_dec) {
  if (arg_dec == 48) return "0";
  if (arg_dec == 49) return "1";
  if (arg_dec == 50) return "2";
  if (arg_dec == 51) return "3";
  if (arg_dec == 52) return "4";
  if (arg_dec == 53) return "5";
  if (arg_dec == 54) return "6";
  if (arg_dec == 55) return "7";
  if (arg_dec == 56) return "8";
  if (arg_dec == 57) return "9";
}
#include  
#include 

//constante des leds
const int led[] = {A0,A1,A2,2,3,4,5,6,7,8,9,10,11,12,13};
const int nbLed = 15;
//donnée des clignotement pour les leds
int ledBlink[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

//donne entrant du port série
String inputSerial = "";
//initialisation de l'écran
LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  //mise en place de l'écran
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0,0);

  //ecoute du port série
  Serial.begin(9600);

  //initialisation des leds et leur mode
  for (int i = 0; i < nbLed; i++) {
    pinMode(led[i], OUTPUT);
    digitalWrite(led[i], HIGH);
  }
}

void loop() {
  for (int i = 0; i < nbLed; i++) {
    if (ledBlink[i] == 1)
      digitalWrite(led[i], LOW);
  }
  delay(200);
  for (int i = 0; i < nbLed; i++) {
    if (ledBlink[i] == 1)
      digitalWrite(led[i], HIGH);
  }
  delay(200);
}

void serialEvent() {
  //récupération des données
  inputSerial = "";
  while (Serial.available() > 0)
  {
    inputSerial = inputSerial + ascii(Serial.read());
  }

  //lecture de la donnée entrante et allumage des LED
  int i;
  for (i = 0; i < nbLed; i++) {
    if (inputSerial.substring(i, i+1) == "0") {
      digitalWrite(led[i], HIGH);
      ledBlink[i] = 0;
    } else if (inputSerial.substring(i, i+1) == "1") {
      digitalWrite(led[i], LOW);
      ledBlink[i] = 0;
    } else if (inputSerial.substring(i, i+1) == "2") {
      ledBlink[i] = 1;
    }
  }
  //gestion de l'écran
  setLcd(inputSerial.substring(i, i+1), inputSerial.substring(i+1, i+4), inputSerial.substring(i+4, i+7));
}

/**
 * fonction de changement d'état de l'écran LCD 
 * partie gestion de l'oxygen et de la température du liquide de refroidissement
 */
void setLcd(String arg_options, String arg_num1, String arg_num2) {
  //réinitialisation de l'écran
  lcd.setCursor(0,0);
  lcd.print("                ");
  lcd.setCursor(0,1);
  lcd.print("                ");  
  lcd.setCursor(0,0);
  
  //message selon option si option 0 aucun affichage
  if ( arg_options == "1" ) {
    lcd.print("No connection");
    lcd.setCursor(0,1);
    lcd.print("on APU");
  } 
  if ( arg_options == "2" ) {
    lcd.print("!!Warning!!");
    lcd.setCursor(0,1);
    lcd.print("Energy low");
  }
  if (arg_options == "3") {
    lcd.print("APU in");
    lcd.setCursor(0,1);
    lcd.print("progress...");
  }
  if ( arg_options == "4") {
    lcd.print("Level : ");
    lcd.setCursor(0,1);
    lcd.print("Use : ");
    lcd.setCursor(9,0);
    lcd.print(arg_num1 + " %");
    lcd.setCursor(9,1);
    lcd.print(arg_num2 + " %");
  }   
}

/**
 * fonction de traduction des données entrant en asciis
 */
String ascii(int arg_dec) {
  if (arg_dec == 48) return "0";
  if (arg_dec == 49) return "1";
  if (arg_dec == 50) return "2";
  if (arg_dec == 51) return "3";
  if (arg_dec == 52) return "4";
  if (arg_dec == 53) return "5";
  if (arg_dec == 54) return "6";
  if (arg_dec == 55) return "7";
  if (arg_dec == 56) return "8";
  if (arg_dec == 57) return "9";
}
--Programme lua pour l'interaction avec PIGOU et Orbiter
--Pour l'interaction en entrée lua procède au découpage d'un code en 2 parties
--MODE,VALEUR

-----------------------FUNCTION------------------------------------------

function split(inputstr, sep)
    if sep == nil then
            sep = "%s"
    end
    local t={} ; i=1
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
            t[i] = str
            i = i + 1
    end
    return t
end

function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

-----------------------INIT----------------------------------------------

local msg = oapi.create_annotation()
--initialisation variable socket
local s = require("socket")
local tcp = assert(socket.tcp())
--la connection doit être établi pour poursuivre
tcp:connect("127.0.0.1", 14000);
tcp:settimeout(0)
--initialisation des valeurs pour le vaisseau
local v = vessel.get_interface('GL-01') --préciser le nom du vaisseau
local vMainFuel = v:get_propellanthandle(0) --réservoir principal
local vRcsFuel = v:get_propellanthandle(1) --réservoir RCS
portDock = v:get_dockhandle(0) --objet du port de docking du vaisseau
--variable de controle blocage des réservoirs
local currentFuelMain = 0
local currentFuelRcs = 0
local fuelMainIsLock = 0
local fuelRcsIsLock = 0
--variable de controle remplissage carburant
local fuelMainIsSupply = 0
local fuelRcsIsSupply = 0
--variable de controle vidange carburant
local fuelMainIsDump = 0
local fuelRcsIsDump = 0
--variable de controle transfert de carburant
local fuelTransfert = 0 --0 rien, 1 main vers rcs, 2 rcs vers main


------------------------MAIN PROGRAM------------------------------

-- ce circuit fonctionnera en permanence
local boucle = true
repeat

	--------------------INPUT----------------------------
	local s, status, partial = tcp:receive()
	if s == nil then
	else
		--recuperation valeur
		local r = split(s,",")
		local mode = r[1]
		local value = r[2]

		--message entrant
		if mode == "MSG" then
			msg:set_text(value) 
		end

		--blocage carburant
		if mode == "FUELLOCK" then
			if (value == "MAIN") then
				if fuelMainIsLock == 0 then
					currentFuelMain = round(v:get_propellantmass(vMainFuel))
					v:set_propellantmass(vMainFuel, 0)
					fuelMainIsLock = 1
				else
					v:set_propellantmass(vMainFuel, currentFuelMain)
					fuelMainIsLock = 0
				end
			end
			if (value == "RCS") then
				if fuelRcsIsLock == 0 then
					currentFuelRcs = round(v:get_propellantmass(vRcsFuel))
					v:set_propellantmass(vRcsFuel, 0)
					fuelRcsIsLock = 1
				else
					v:set_propellantmass(vRcsFuel, currentFuelRcs)
					fuelRcsIsLock = 0
				end
			end
		end
		
		--switch des états
		if mode == "FUELSUPPLY" then
			if (value == "MAIN") then
				if fuelMainIsSupply == 1 then
					fuelMainIsSupply = 0
				else
					fuelMainIsSupply = 1
				end
			end
			if (value == "RCS") then
				if fuelRcsIsSupply == 1 then
					fuelRcsIsSupply = 0
				else
					fuelRcsIsSupply = 1
				end
			end
		end
		if mode == "FUELDUMP" then
			if (value == "MAIN") then
				if fuelMainIsDump == 1 then
					fuelMainIsDump = 0
				else
					fuelMainIsDump = 1
				end
			end
			if (value == "RCS") then
				if fuelRcsIsDump == 1 then
					fuelRcsIsDump = 0
				else
					fuelRcsIsDump = 1
				end
			end
		end
		if mode == "FUELTRANSFERT" then
			if value == "MAINTORCS" then
				fuelTransfert = 1
			end
			if value == "RCSTOMAIN" then
				fuelTransfert = 2
			end
			if value == "NONE" then
				fuelTransfert = 0
			end
		end

		--commande d'action du vaisseau
		if mode == "CMD" then
			--changement mode HUD
			if value == "HUDMODE" then
				v:send_bufferedkey(OAPI_KEY.H)
			end
			--changement mode RCS linéaire ou rotation
			if value == "RCSMODE" then
				v:send_bufferedkey(OAPI_KEY.DIVIDE)
			end
			--dedocking
			if value == "DEDOCK" then
				v:undock()
			end
			--commande auto pilote, kill rot, pro, retro, level horizon, orbit+, orbit-
			if value == "APLH" then
				v:send_bufferedkey(OAPI_KEY.L)
			end
			if value == "APPRO" then
				v:send_bufferedkey(OAPI_KEY.LBRACKET)
			end
			if value == "APRETRO" then
				v:send_bufferedkey(OAPI_KEY.RBRACKET)
			end
			if value == "APORBITMORE" then
				v:send_bufferedkey(OAPI_KEY.SEMICOLON)
			end
			if value == "APORBITLESS" then
				v:send_bufferedkey(OAPI_KEY.APOSTROPHE)
			end
			if value == "APKILLROT" then
				v:send_bufferedkey(OAPI_KEY.NUMPAD5)
			end
			if value == "APHOLDALTITUDE" then
				v:send_bufferedkey(OAPI_KEY.A)
			end

			--ouverture/fermeture nez de l'appareil
			if value == "NOSE" then
				v:send_bufferedkey(OAPI_KEY.K)
			end
			--ouverture/fermeture train d'atterissage
			if value == "GEAR" then
				v:send_bufferedkey(OAPI_KEY.G)
			end

			oapi.dbg_out(s)
			s = nil
		end
	end
	--effectue le plein si possible
	if fuelMainIsSupply == 1 then
		v:set_propellantmass(vMainFuel, v:get_propellantmass(vMainFuel) + 50)
		if v:get_propellantmass(vMainFuel) > 9600 then
			v:set_propellantmass(vMainFuel, 9600)
		end
	end
	if fuelRcsIsSupply == 1 then
		v:set_propellantmass(vRcsFuel, v:get_propellantmass(vRcsFuel) + 20)
		if v:get_propellantmass(vRcsFuel) > 600 then
			v:set_propellantmass(vRcsFuel, 600)
		end
	end
	--vidange
	if fuelMainIsDump == 1 then
		v:set_propellantmass(vMainFuel, v:get_propellantmass(vMainFuel) - 50)
	end
	if fuelRcsIsDump == 1 then
		v:set_propellantmass(vRcsFuel, v:get_propellantmass(vRcsFuel) - 20)
	end
	--transfert carburant
	if fuelTransfert > 0 then
		--mode transfert main to rcs
		if fuelTransfert == 1 then
			if v:get_propellantmass(vMainFuel) > 0 then
				v:set_propellantmass(vRcsFuel, v:get_propellantmass(vRcsFuel) + 10)
				v:set_propellantmass(vMainFuel, v:get_propellantmass(vMainFuel) - 10)
				--arrêt du transfert si plus de jus ou reservoir entrant plein
				if v:get_propellantmass(vMainFuel) < 0 or v:get_propellantmass(vRcsFuel) >= 600 then
					fuelTransfert = 0
				end
				if v:get_propellantmass(vRcsFuel) > 600 then
					v:set_propellantmass(vRcsFuel, 600)
				end
			end
		end
		--mode transfert rcs to main
		if fuelTransfert == 2 then
			if v:get_propellantmass(vRcsFuel) > 0 then
				v:set_propellantmass(vMainFuel, v:get_propellantmass(vMainFuel) + 10)
				v:set_propellantmass(vRcsFuel, v:get_propellantmass(vRcsFuel) - 10)
				--arrêt du transfert si plus de jus ou reservoir entrant plein
				if v:get_propellantmass(vRcsFuel) < 0 or v:get_propellantmass(vMainFuel) >= 9600 then
					fuelTransfert = 0
				end
				if v:get_propellantmass(vMainFuel) > 9600 then
					v:set_propellantmass(vRcsFuel, 9600)
				end
			end
		end
	end
	
	--------------------OUTPUT----------------------------
	-------sortie séparé par des virugules : ordre ALTITUDE,MAINFUEL,RCSFUEL

	local output = ""

	--altitude du vaisseau
	output = output .. round(v:get_altitude()) .. ","

	--état du carburant sur le simulateur par rapport au blocage carburant
	if fuelMainIsLock == 1 then
		output = output .. currentFuelMain
	else
		output = output .. round(v:get_propellantmass(vMainFuel))
	end
	if fuelRcsIsLock == 1 then
		output = output .. "," .. currentFuelRcs
	else
		output = output .. "," .. round(v:get_propellantmass(vRcsFuel))
	end
	--etat du docking si le vaiseau est arrimé
	local isDock = 1
	if (v:get_dockstatus(portDock) == nil) then
		isDock = 0
	end
	output = output .. "," .. isDock .. "\n"
	tcp:send(output)

	--en seconde précise le temps d'accorder du temps au simulateur
	proc.wait_simdt(0.5)
until boucle == false