Hi,
I'm working on an installation with two screens, arduino loaded with firmata. A sketch that creates two windows, (simplified code posted here question about class-PShapes) This really pushes my macbook pro to its limits.
Everytime I put something extra in, even as simple as a shape or a drawn rectangle of sorts, it strains mijn MBP even more. As soon as I start the sketch up the temperature skyrockets to 95degrees celsius, making the fans put out 6000RPM in order to keep it from overheating. According to activity monitor the sketch takes up 180% of CPU power.
Right now this is my setup:
Arduino Loaded with Firmata, coupled with two sensors, one Sharp IR-sensor and one Maxbotix Ultrasonic. Three pushbuttons. All adequately connected. USB connection to MBP in order to collect recieved data.
1 Main sketch, initiating a second window class called UIwindow which extends PApplet
- enables arduino's functions by communicating with the Firmatacode loaded on the arduino
- handles pushbuttons and sensors, only reads data
- converts read data to simple forms, like a circle.
- loads one SVG-shape
this all happens in one window of 1280x1024
2 the UIWindow class, which creates another window at 1440 X900
- loads two SVG shapes
- outputs text on screen with data from one sensor.
- converts a rect to another color when a button is pressed.
The code:
MAIN
The UIWINDOW
I want to connect a webcam with face recognition on it later on, but this makes me worry. alot.
Any help regarding this is very much appreciated. I remember using an external powersource to use with Arduino help stabilize sensor readings, perhaps that would help with the sketchload as well. I will try that next.
EDIT ------------------------------------------
I've reviewed the code with help of a friend and it seems that the external window is the culprit. When I disable the external screen it settles to 100%CPU usage. It's still 100% though...
EDIT #2 ---------------------------------------
I managed to track the problem to the arduino and it's sensors. While it's not necessarily their fault, you absolutely need to constrain the amount of sensor readings it does. I put in a delay of (100) in both classes' draw function, and succesfully reduced CPU usage to 80%.
I'm working on an installation with two screens, arduino loaded with firmata. A sketch that creates two windows, (simplified code posted here question about class-PShapes) This really pushes my macbook pro to its limits.
Everytime I put something extra in, even as simple as a shape or a drawn rectangle of sorts, it strains mijn MBP even more. As soon as I start the sketch up the temperature skyrockets to 95degrees celsius, making the fans put out 6000RPM in order to keep it from overheating. According to activity monitor the sketch takes up 180% of CPU power.
Right now this is my setup:
Arduino Loaded with Firmata, coupled with two sensors, one Sharp IR-sensor and one Maxbotix Ultrasonic. Three pushbuttons. All adequately connected. USB connection to MBP in order to collect recieved data.
1 Main sketch, initiating a second window class called UIwindow which extends PApplet
- enables arduino's functions by communicating with the Firmatacode loaded on the arduino
- handles pushbuttons and sensors, only reads data
- converts read data to simple forms, like a circle.
- loads one SVG-shape
this all happens in one window of 1280x1024
2 the UIWindow class, which creates another window at 1440 X900
- loads two SVG shapes
- outputs text on screen with data from one sensor.
- converts a rect to another color when a button is pressed.
The code:
MAIN
- import processing.serial.*;
import hypermedia.video.*;
import java.awt.Rectangle;
import cc.arduino.*;
import processing.opengl.*;
OpenCV opencv;
Arduino arduino;
UIWindow uiWindow;
PShape oogSVG;
PShape infoSVG;
// Images must be in the "data" directory to load correctly
// videocapture
int contrast_value = 0;
int brightness_value = 0;
// Kleuren (R G B)
color blauw = color(0,0,180);
color groen = color(0,180,0);
color zwart = color(0,0,0);
color grijs = color(200,200,200);
boolean sonicActive = false;
boolean infraActive = false;
boolean camActive = false;
//////////////////////////////////////////////////////////////////
///////////INITIALIZE//////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
public void init() {
frame.removeNotify();
frame.setUndecorated(true);
frame.addNotify();
super.init();
}
/////////////////////////////////////////////////////////////////
//////////////////SETUP//////////////////////////////////////////
/////////////////////////////////////////////////////////////////
void setup() {
size(1280, 1024);
// Passing in this applet allows you to call functions in the parent
uiWindow = new UIWindow( this, 1440, 900 );
frame.setLocation(0,0);
oogSVG = loadShape ("oog1.svg");
infoSVG = loadShape ("kader1.svg");
smooth();
arduino = new Arduino(this, Arduino.list()[0], 57600);
// INPUTS
arduino.pinMode(2, Arduino.INPUT);
arduino.pinMode(12, Arduino.INPUT);
arduino.pinMode(11, Arduino.INPUT);
arduino.pinMode(10, Arduino.INPUT);
}
public void stop() {
opencv.stop();
super.stop();
}
public Arduino getArduinoClass(){
return arduino;
}
////////////////*********************////////////////////////////
////////// DRAW //////////////////////////////////////
////////////////*********************////////////////////////////
void draw() {
background(200);
noFill();
shape(oogSVG, 0, 0);
// Buttons
if (arduino.digitalRead(2) == Arduino.HIGH && !sonicActive) {
sonicActive = true;
infraActive = false;
camActive = false;
}
if (arduino.digitalRead(3) == Arduino.HIGH && !infraActive) {
infraActive = true;
camActive = false;
sonicActive = false;
}
if (arduino.digitalRead(4) == Arduino.HIGH && !camActive) {
camActive = true;
sonicActive = false;
infraActive = false;
}
if (sonicActive == true) {
sonicDist();
}
if (infraActive == true) {
infraDist();
}
if (camActive == true) {
// camActive();
}
}
/////////////////////////////////////////////////////////////////
////////// IR-SENSOR ///////////////////////////////////
/////////////////////////////////////////////////////////////////
// if (
void infraDist()
{
fill(groen);
noStroke();
float val = arduino.analogRead(4);
float volts = arduino.analogRead(4)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 65*pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
float factor = 500/170;
val = 500 - val;
float eyeSize = val * factor;
ellipseMode(CENTER);
ellipse (350, 250, eyeSize, eyeSize);
}
/////////////////////////////////////////////////////////////////
////////// ULTRASOON-SENSOR ////////////////////////////
/////////////////////////////////////////////////////////////////
void sonicDist()
{
fill(zwart);
float val = arduino.analogRead(3);
val = 80 - val;
float eyeSize = val+80;
ellipseMode(CENTER);
ellipse(600, 430 , eyeSize, eyeSize);
}
The UIWINDOW
- public class UIWindow extends PApplet
{
Frame frame;
int width;
int height;
Arduino arduino;
PShape infoSVG;
PShape oogKleinSVG;
Main parent;
PFont font;
UIWindow ( Main _parent, int w, int h )
{
parent = _parent;
arduino = parent.getArduinoClass();
width = w;
height = h;
frame = new Frame( );
frame.setBounds( 0, 0, width, height );
frame.removeNotify();
frame.setUndecorated(true);
frame.setLocation( 0, 0 );
frame.add( this);
this.init( );
frame.show( );
}
void setup( )
{
size( width, height );
frameRate( 10 );
font = parent.loadFont("NewsGothicMT-42.vlw");
infoSVG = parent.loadShape ("kader1.svg");
oogKleinSVG = parent.loadShape("oog1.svg");
smooth();
}
void draw ( )
{
background(255, 255, 220);
shape(infoSVG, 0, 0);
shape(oogKleinSVG, 80, 450, 500, 400);
sensorActive(200, 0, 0);
textFont(font);
textMode(SCREEN);
fill(0);
text("SENSOR ACTIEF: ", 110, 100);
if (sonicActive) {
float sonicVal = parent.arduino.analogRead(3);
float inches = 0;
float cm = 0;
sensorActive(0, 200, 0);
inches = ((sonicVal*0.0049)/0.0098);
cm= inches * 2.54;
fill(0);
//textSize(20);
text("Afstand tot object: "+cm + " CM", 100, 200);
}
}
void sensorActive(int rood, int groen, int blauw)
{
fill(rood, groen, blauw);
rect(515, 45, 70, 70);
}
}
I want to connect a webcam with face recognition on it later on, but this makes me worry. alot.
Any help regarding this is very much appreciated. I remember using an external powersource to use with Arduino help stabilize sensor readings, perhaps that would help with the sketchload as well. I will try that next.
EDIT ------------------------------------------
I've reviewed the code with help of a friend and it seems that the external window is the culprit. When I disable the external screen it settles to 100%CPU usage. It's still 100% though...
EDIT #2 ---------------------------------------
I managed to track the problem to the arduino and it's sensors. While it's not necessarily their fault, you absolutely need to constrain the amount of sensor readings it does. I put in a delay of (100) in both classes' draw function, and succesfully reduced CPU usage to 80%.
1