Annonce

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


#1 2009-04-26 19:19:46 Contrôle vitesse dessin dans processing

prahaline
nouveau membre
Date d'inscription: 2009-04-26
Messages: 5

Contrôle vitesse dessin dans processing



Bonjour à tous,

Je travaille en ce moment sur les Lsystems dans processing (voir exemples donnés avec le logiciel). Après avoir créer les codes uniquement sur processing j'aimerais maintenant controler la vitesse de dévelopement des dessins avec Arduino. Me basant sur les exemples de la partie 'serial communication' (sur http://www.arduino.cc/) j'ai commencé à réécrire le code mais ils semblerait que la fonction delay() ne peut pas être définie en tant que int ou float et ne peut donc être contrôlée par arduino.

Avez vous donc une autre idée qui me permettrait d'obtenir un résultat??

(ps : la fonction delay() fonctionne lorsque je le défini par un nombre fixe dans processing seulement)

voici les codes :


POUR ARDUINO :

void setup() {

  Serial.begin(9600);
}

void loop() {

  Serial.println(analogRead(0));

  delay(10);
}

POUR PROCESSING : (un code général et deux class)

Code (p5) :

//CODE GENERAL  :

import processing.serial.*;

float delayValue=0;

Serial myPort;


PentigreeLSystem ps;

void setup() {

  size(360, 640);
  smooth();
  ps = new PentigreeLSystem();
  ps.simulate(6);

  println(Serial.list());
  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.bufferUntil('\n');
}

void draw() {
  background(0);
  translate (-100,-250);
  ps.render();
  delay (delayValue);
}

void serialEvent (Serial myPort) {

  String inString = myPort.readStringUntil('\n');
  if (inString != null) {

    inString = trim(inString);
    delayValue = map(inString, 0, 1023, 1000, 0);

  }
}

//LSYSTEM (1ere class) :

class LSystem {

  int steps = 0;

  String axiom;
  String rule;
  String production;

  float startLength;
  float drawLength;
  float theta;

  int generations;

  LSystem() {

    axiom = "F";
    rule = "F+F-F";
    startLength = 90.0;
    theta = radians(120.0);
    reset();
  }

  void reset() {
    production = axiom;
    drawLength = startLength;
    generations = 0;
  }

  int getAge() {
    return generations;
  }
 
  void render() {
    translate(width/2, height/2);
    steps +=1000;         
    if (steps > production.length()) {
      steps = production.length();
    }
    for (int i = 0; i < steps; i++) {
      char step = production.charAt(i);
      if (step == 'F') {
        rect(0, 0, -drawLength, -drawLength);
        noFill();
        translate(0, -drawLength);
      }
      else if (step == '+') {
        rotate(theta);
      }
      else if (step == '-') {
        rotate(-theta);
      }
      else if (step == '[') {
        pushMatrix();
      }
      else if (step == ']') {
        popMatrix();           
      }
    }
  }
 
  void simulate(int gen) {
    while (getAge() < gen) {
      production = iterate(production, rule);
    }
  }

  String iterate(String prod_, String rule_) {
    drawLength = drawLength * 0.6;
    generations++;
    String newProduction = prod_;         
    newProduction = newProduction.replaceAll("F", rule_);
    return newProduction;
  }
}

//PENTIGREE LSYSTEM (2e class)

class PentigreeLSystem extends LSystem {

  int steps = 0;
  float somestep = 0.1;
  float xoff = 0.01;

  PentigreeLSystem() {
    axiom = "F-F-F-F-F";
    rule = "F-F++F+F-F-F";
    startLength = 60.0;
    theta = radians(72); 
    reset();
  }

  void useRule(String r_) {
    rule = r_;
  }

  void useAxiom(String a_) {
    axiom = a_;
  }

  void useLength(float l_) {
    startLength = l_;
  }

  void useTheta(float t_) {
    theta = radians(t_);
  }

  void reset() {
    production = axiom;
    drawLength = startLength;
    generations = 0;
  }

  int getAge() {
    return generations;
  }

  void render() {
    translate(width/4, height/2);
    steps += 3;         
    if (steps > production.length()) {
      steps = production.length();
    }

    for (int i = 0; i < steps; i++) {
      char step = production.charAt(i);
      if (step == 'F') {
        noFill();
        stroke(255);
        line(0, 0, 0, -drawLength);
        translate(0, -drawLength);
      }
      else if (step == '+') {
        rotate(theta);
      }
      else if (step == '-') {
        rotate(-theta);
      }
      else if (step == '[') {
        pushMatrix();
      }
      else if (step == ']') {
        popMatrix();
      }
    }
  }

Hors ligne

 

#2 2009-04-26 23:06:30 Re : Contrôle vitesse dessin dans processing

emoc
@#@*$
Lieu: Quimper
Date d'inscription: 2008-01-28
Messages: 1576
Site web

Re: Contrôle vitesse dessin dans processing



Bonjour et bienvenue,

La valeur que tu reçois depuis arduino est une chaine de caractère, mais elle peut être transformée en entier

String a = "100";
int b = Integer.parseInt(a);
println(b);

ou en float

String a = "100.96";
float b = Float.parseFloat(a);
println(b);

En plus des fonctions de processing, on peut utiliser toutes les fonctions java. Elles ne sont pas indiquées dans la documentation de processing, mais tu peux les trouver ici : http://java.sun.com/j2se/1.4.2/docs/api/

En modifiant cette partie de ton code, ça devrait fonctionner

void serialEvent (Serial myPort) {

  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    inString = trim(inString);
    float a = Float.parseFloat(inString);
    delayValue = map(a, 0, 1023, 1000, 0);
  }
}

Hors ligne

 

#3 2009-04-27 17:24:34 Re : Contrôle vitesse dessin dans processing

prahaline
nouveau membre
Date d'inscription: 2009-04-26
Messages: 5

Re: Contrôle vitesse dessin dans processing



Apparement, delay() doit automatiquement être défini en integer car quand j'utilise le converteur float processing me dit "the method delay(int) in the type PApplet is not applicable for the arguments (float). J'utilise donc cette patie du code :

void serialEvent (Serial myPort) {

  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    inString = trim(inString);
    int a = Integer.parseInt(inString);
    delayValue= map(a, 0, 1023, 1000, 0);

  }
  
}

Mais ça ne marche toujours pas car quand je lance le code entier, processing m'indique une erreur : "Cannot convert from float to int"

LE CODE EN ENTIER :

Code (p5) :

import processing.serial.*;


int delayValue=0;

Serial myPort;


PentigreeLSystem ps;

void setup() {

  size(360, 640);
  smooth();
  ps = new PentigreeLSystem();
  ps.simulate(6);

  println(Serial.list());
  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.bufferUntil('\n');
}




void serialEvent (Serial myPort) {

  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    inString = trim(inString);
    int a = Integer.parseInt(inString);
    delayValue= map(a, 0, 1023, 1000, 0);

  }
  
}

void draw() {


  background(0);
  translate (-100,-250);
  ps.render();
  delay(delayValue);


}

Hors ligne

 

#4 2009-04-29 14:39:26 Re : Contrôle vitesse dessin dans processing

emoc
@#@*$
Lieu: Quimper
Date d'inscription: 2008-01-28
Messages: 1576
Site web

Re: Contrôle vitesse dessin dans processing



Map() utilise des valeurs flottantes, c'est probablement ça qui coince. ll faut d'abord convertir ta valeur de String en float, puis le résultat de map() de float en int pour que delayValue soit un nombre entier

void serialEvent (Serial myPort) {
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    inString = trim(inString);
    float a = Float.parseFloat(inString);
    float b = map(a, 0, 1023, 1000, 0);
    delayValue = (int)b;
  }
}

Hors ligne

 

#5 2009-04-29 22:32:28 Re : Contrôle vitesse dessin dans processing

prahaline
nouveau membre
Date d'inscription: 2009-04-26
Messages: 5

Re: Contrôle vitesse dessin dans processing



ouais!Hourah!! ça marche!!Quelques ajustements à faire mais je pense que ça ira....Merci beaucoup!! J'adore votre site...les réponses sont si rapides....

Hors ligne

 

fil rss de cette discussion : rss

Pied de page des forums

Powered by FluxBB

codelab, graphisme & code : emoc / 2008-2024