Annonce

>>> Bienvenue sur codelab! >>> Première visite ? >>> quelques mots sur codelab //// une carte des membres//// (apéros) codelab


#1 2016-07-12 20:27:00 Rapidité d'exécution - communication arduino

tyboth
nouveau membre
Date d'inscription: 2016-07-12
Messages: 3

Rapidité d'exécution - communication arduino



Bonjour,

J'ai découvert processing par hasard en faisant quelques recherches pour mon projet de drone contrôlé par arduino. Mais avant j'aimerai pouvoir contrôler mon drone avec une manette et pour cela j'utilise une library spéciale pour gamepad et je communique ensuite grâce au serial de l'arduino.

Dans mon programme processing, j’utilise un index, c'est à dire que je vérifie l'état d'un seul des bouton à chaque itération de la boucle, ce qui permet d'éviter que certains boutons soient prioritaire sur d'autres, bref ça règle pas mal de problèmes liés à la communication. Cependant le défaut qui apparait c'est que si je fais une impulsion trop brève sur un des boutons il peux ne pas être détecté. J'ai vérifié la durée de la boucle sur processing et arduino et les 2 sont de l'ordre de 1 milliseconde donc pas de problèmes de ce point de vue. J'ai aussi augmenté le débit du serial au maximum ie 115200 bits/s d'après le site arduino mais ça ne change rien au problème.

Si vous avez une piste à me proposer je suis preneur merci.

Hors ligne

 

#2 2016-07-12 20:50:53 Re : Rapidité d'exécution - communication arduino

pob
Exterminator
Lieu: Rennes
Date d'inscription: 2009-10-13
Messages: 765
Site web

Re: Rapidité d'exécution - communication arduino



Bienvenue Tyboth,

J'ai déplacé ton message dans le forum Processing puisqu'il s'agit a priori d'une question relative à Processing.
Je pense que c'e serait bien plus facile de t'aider avec ton code sous les yeux. wink

Hors ligne

 

#3 2016-07-13 19:19:25 Re : Rapidité d'exécution - communication arduino

tyboth
nouveau membre
Date d'inscription: 2016-07-12
Messages: 3

Re: Rapidité d'exécution - communication arduino



Merci.
Voici mes codes:

#########################Processing###########################

import net.java.games.input.*;
import org.gamecontrolplus.*;
import org.gamecontrolplus.gui.*;
import java.util.*;
import java.nio.*;

import processing.serial.*;

ControlIO control;
Configuration config;
ControlDevice gpad;

//Boutons
boolean Start;
boolean Select;
boolean A;
boolean B;
boolean X;
boolean Y;
boolean LB;
boolean RB;
boolean L3;
boolean R3;

//Axes des joysticks
int X1;
int Y1;
int X2;
int Y2;
int Z;

int index;

//Mesure de temps
long start;
long end;
long dif;

Serial port;
########################Setup#################
void setup() {
  size(256, 150);
 
  index =0;
 
  control = ControlIO.getInstance(this); //Initialise the controlIO
  gpad = control.getMatchedDevice("gamepad"); //find device that match the configuration file
  if (gpad == null){
    println("No suitable device configurated");
    System.exit(-1); //End the program
  }

  println("Available serial ports:");
  println(Serial.list());
 
  port = new Serial(this, "COM3", 115200);
}
############################################
#####################Draw######################
void draw() {
  //index =10;
  start = System.currentTimeMillis();

  Start = gpad.getButton("Start").pressed();
  Select = gpad.getButton("Select").pressed();
  A = gpad.getButton("A").pressed();
  B = gpad.getButton("B").pressed();
  X = gpad.getButton("X").pressed();
  Y = gpad.getButton("Y").pressed();
  LB = gpad.getButton("LB").pressed();
  RB = gpad.getButton("RB").pressed();
  L3 = gpad.getButton("L3").pressed();
  R3 = gpad.getButton("R3").pressed();
 
  X1 = (int)(gpad.getSlider("Axe X").getValue()*100);
  Y1 = (int)(gpad.getSlider("Axe Y").getValue()*100);
  X2 = (int)(gpad.getSlider("Rotation X").getValue()*100);
  Y2 = (int)(gpad.getSlider("Rotation Y").getValue()*100);
 
  if (A){
    pA = true;
    if (index==0){
      port.write(0);
      pA = false;
    }
  }
  if (B && index==1){
    port.write(1);
  }
  if (X && index==2){
    port.write(2);
  }
  if (Y && index==3){
    port.write(3);
  }
  if (LB && index==4){
    port.write(4);
  }
  if (RB && index==5){
    port.write(5);
  }
  if (Select && index==6){
    port.write(6);
  }
  if (Start && index==7){
    port.write(7);
  }
  if (L3 && index==8){
    port.write(8);
  }
  if (R3 && index==9){
    port.write(9);
  }
  if (index==10){
    port.write(10);
    port.write(150+X1);
  }
  /*if (index==11){
    port.write(351+Y1);
  }
  if (index==12){
    port.write(552+X2);
  }
  if (index==13){
    port.write(753+Y2);
  }*/
 
  System.out.println("A="+A+" B="+B+" X="+X+" Y="+Y+" LB="+LB+" RB="+RB+" Select="+Select+" Start="+Start+" L3="+L3+" R3="+R3+" X1="+X1+" Y1="+Y1+" X2="+X2+" Y2="+Y2);
 
  index++;
  if (index>=13){
    index=0;
  }
  end = System.currentTimeMillis();
  dif = end - start;
 
  System.out.println(dif);
}

##############################################################

#####################Arduino###################################

#include <Wire.h> //bus I²C

#define MPU_9255 0x68//MPU-9255 I²C address

#define ACCEL_XOUT_H 0x3B//Accelerometer I²C address (read only)
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40

#define GYRO_XOUT_H 0x43//Gyroscope I²C address (read only)
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48

#define MAG_XOUT_H 0x03//Magnetometer I²C adress (read only)
#define MAG_XOUT_L 0x04
#define MAG_YOUT_H 0x05
#define MAG_YOUT_L 0x06
#define MAG_ZOUT_H 0x07
#define MAG_ZOUT_L 0x08

#define DATA 2 //serial read
#define WR 3
#define CS 4
#define CS2 5

const int motor1 = 11;//4 motors PWM output
const int motor2 = 10;
const int motor3 = 9;
const int motor4 = 6;
const int motor[4] = {motor1, motor2, motor3, motor4};

const int SDA0 = A4;//Serial Data Line I²C
const int SCL0 = A5;//Serial Clock Line I²C

int varList[35] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //Valeurs des entrées

int ACC_X;
int ACC_Y;
int ACC_Z;

int GYRO_X;
int GYRO_Y;
int GYRO_Z;

int MAG_X;
int MAG_Y;
int MAG_Z;

byte wireBytes[8]; //I²C

int castByteStringToInt (byte Bytes[8]){//I²C
  int result = 0;
  for (int k=0; k<=7;k++){
    if (Bytes[k]==1){
      ACC_X=ACC_X+pow(2,k);
    }
  }
  return result;
}

unsigned long t;
unsigned long tList[36];
int indexNb;
int lastIndex[10];

long start;
long endd;
long diff;

String inString = "";

//##############################Setup####################################
void setup() {
 
  Serial.begin(115200);
  Wire.begin();
 
  analogWrite(motor1, 0);
  analogWrite(motor2, 0);
  analogWrite(motor3, 0);
  analogWrite(motor4, 0);

  ACC_X=0;
  ACC_Y=0;
  ACC_Z=0;

  GYRO_X=0;
  GYRO_Y=0;
  GYRO_Z=0;

  MAG_X=0;
  MAG_Y=0;
  MAG_Z=0;

  t = millis();
  for (int k=0; k<=35; k++){
    tList[k]=t;
  }

  indexNb = 0;

}
//########################################################################
//#############################loop#####################################
void loop() {

  start = millis();

  t = millis();

  byte proc;//data from pc
  if (Serial.available()){
    proc = Serial.read();


    if (proc==10){
      indexNb++;
      //Serial.println(indexNb);
    }

   
   
    for (int k=0; k<=9; k++){
      if (proc==k){
        varList[k] = 1;
        tList[k] = t;
        lastIndex[k]=indexNb;
        //analogWrite(motor1,k*20);
      }
      else if (indexNb-lastIndex[k]>1){
        varList[k] = 0;
      }
    }
    for (int k=10; k<=13; k++){
      if (50<int(proc)<250){
       
        varList[k] = max(0,int(proc) - 50);
        analogWrite(motor1,varList[10]);
      }
    }
/*//Test
    if (proc==0){
      analogWrite(motor1,255);
    }
    else{
      analogWrite(motor1,0);
    }
*/
    /*
    analogWrite(motor1,255*varList[0]);//Test boutons
    analogWrite(motor2,255*varList[1]);
    analogWrite(motor3,255*varList[2]);
    analogWrite(motor4,255*varList[3]);*/

    //analogWrite(motor1,(100+varList[10]);//Test axes joysticks
    /*analogWrite(motor2,(100+varList[11]));
    analogWrite(motor3,(100+varList[12]);
    analogWrite(motor4,(100+varList[13]);*/
   
    //analogWrite(motor1,10);//Test simple
   
  }
//Commandes du bus I2C
  Wire.beginTransmission(MPU_9255);
  Wire.write(ACCEL_XOUT_H);
  Wire.endTransmission(false);
  Wire.requestFrom(ACCEL_XOUT_H, 8);
  for (int k=0; k<=7; k++){
    wireBytes[k] = Wire.read();
  }
  ACC_X=castByteStringToInt(wireBytes);


  endd = millis();
  diff=endd-start;
  //Serial.println(diff);
}

#####################################################################

Hors ligne

 

#4 2016-07-13 19:36:40 Re : Rapidité d'exécution - communication arduino

tyboth
nouveau membre
Date d'inscription: 2016-07-12
Messages: 3

Re: Rapidité d'exécution - communication arduino



Bon j'ai réglé mon problème en mettant simplement une boucle while dans le draw.

Hors ligne

 

fil rss de cette discussion : rss

Pied de page des forums

Powered by FluxBB

codelab, graphisme & code : emoc / 2008-2024