
como ultimo ejercicio propuesto para el taller datajockey, los códigos para processing y arduino abajo generan una representación visual de dos fenómenos distintos: en primero lugarm la imágen de una cámara conectada a una computadora es cambiada, via processing, en una matriz de pixels coloridos. estos pixels tienen sus propriedades modificadas en tiempo real, segundo la distancia de un objeto en relación a un sonar, conectado a una placa arduino.
el código para aplicación en processing es este. como biblioteca de vídeo, se eligió gsvideo, ya que es baseada en gstreamer, portanto, funciona en linux.
//camera-sonar module visualization //by medul.la //based on the example 'mirror', of the gsvideo processing library, //and the code found in this forum post by dvnanness: //http://forum.processing.org/topic/multiple-sonar-reading-from-arduino-to-processing import processing.serial.*; import codeanticode.gsvideo.*; // Number of columns and rows in our system int cols, rows; // Variable for capture device GSCapture video; Serial myPort; int numSensors = 1; int linefeed = 10; int sensors[]; float read1; int cellZFactor; // Size of each cell in the grid int cellSize = 15; void setup() { size(640, 480, P3D); //set up columns and rows cols = width / cellSize; rows = height / cellSize; colorMode(RGB, 255, 255, 255, 100); rectMode(CENTER); // uses the default video input, see the reference if this causes an error video = new GSCapture(this, width, height, 30); // list all the available serial ports println(Serial.list()); // change the 0 to the appropriate number of the serial port // that your microcontroller is attached to. myPort = new Serial(this, Serial.list()[0], 9600); myPort.bufferUntil(linefeed); } void draw() { if (sensors != null) { // if valid data arrays are not null // compare each sensor value with the previuos reading // to establish change read1 = map(sensors[0], 0, 600, 1, 30); cellZFactor = int(read1); } if (video.available()) { video.read(); video.loadPixels(); background(0); // begin loop for columns for (int i = 0; i < cols;i++) { // begin loop for rows for (int j = 0; j < rows;j++) { // where are we, pixel-wise? int x = i * cellSize; int y = j * cellSize; int loc = (video.width - x - 1) + y*video.width; // reversing x to mirror the image // the rects' color and z position depends on the information from the sonar input, // the brightness and the colors captured by the camera color c = video.pixels[loc]; float sz = (brightness(c) / 255.0) * cellSize + cellZFactor; fill(red(c)/cellZFactor,(blue(c)+(cellZFactor)), (green(c)*(cellZFactor)/3)); noStroke(); rect(x + cellSize/2, y + cellSize/2, sz, sz); } } } } void serialEvent(Serial myPort) { // read the serial buf //camera-sonar mfer: String myString = myPort.readStringUntil(linefeed); // if you got any bytes other than the linefeed: if (myString != null) { myString = trim(myString); // split the string at the commas // and convert the sections into integers: sensors = int(split(myString, '\n')); // print out the values you got: for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) { print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t"); } // add a linefeed after all the sensor values are printed: println(); } }
y este es el codigo para obtener la información del sonar, por intermedio de un arduino. para el código abajo, el modelo del sonar debe ser similar al HC-SR04.
//defining ports const int pingPin = 7; const int pingPin8 = 8; void setup() { Serial.begin(9600); } void loop(){ long duration, inches, cm; pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin8, INPUT); duration = pulseIn(pingPin8, HIGH); inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); Serial.println(cm); delay(100); } long microsecondsToInches(long microseconds){ return microseconds / 74 / 2; } long microsecondsToCentimeters(long microseconds){ return microseconds / 29 / 2; }
más detalles acerca de como enchufar un HC-SR04 en un arduino acá.
Pingback: datajockey in buenos aires | m e d u l . l a / b l o g