PDA

View Full Version : [Question(s)] Processing 2 to Arduino Wireless RS232



BrEtT
05-15-2015, 10:30 PM
Hi,I have been working on this project with my kids for a few years.Hard wired it works fairly well, but now we want it to be wireless. ControlP5 i have found to be easy to use, but now going wireless i cant get the arduino to interpret the signal.The signal being sent from Processing is (a127) a is the servo id ,127 is the servo position the servo position numbers did change, checked by adding a println.I have tried chars, int, trying to read string read bytes was even thinking maybe to separate the 'a' from servo position. Think now is the time to bite the bullet and ask for some help .
Wireless device= RF Wireless Module Kit USB UART RS232


Arduino code



#include <Servo.h>


Servo pan;
Servo tilt;
Servo lftarm;
Servo rghtarm;
Servo lfthand;
Servo rghthand;
Servo torso;
void setup()
{
lfthand.attach(7);
rghthand.attach(8);
torso.attach(6);
pan.attach(9);
tilt.attach(10);
lftarm.attach(11);
rghtarm.attach(12);


Serial.begin(57600); // opens serial port, sets data rate to 9600 bps
Serial.println("ready");

}
void loop() {
static int v = 0;

if ( Serial.available()) {
char ch = Serial.read();

switch(ch) {
case'0'...'9':
v = v * 10 + ch - '0';
break;
case 'a':
pan.write(v);
Serial.println(v);
v = 0;
break;
case 'b':
tilt.write(v);
Serial.println(v);
v = 0;
break;
case 'c':
lftarm.write(v);
Serial.println(v);
v = 0;
break;
case 'd':
rghtarm.write(v);
Serial.println(v);
v = 0;
break;
case 'e':
lfthand.write(v);
Serial.println(v);
v = 0;
break;
case 'f':
rghthand.write(v);
Serial.println(v);
v = 0;
break;
case 'g':
torso.write(v);
Serial.println(v);
v = 0;
break;

}
}
}





Processing2


import processing.video.*;






import controlP5.*;




import processing.serial.*;









float fps = 2;
Boolean saveFrames = false; // set to false if you don't want the frames saved to file
String fileName = "IMG_"; // a text string that will start the file names of each saved image
String fileType = ".jpg"; // you can choose "tif", "tga", "jpg", "png" Files.
String filePath0 = "E:\\Chris's stuff\\datalogs\\saveFrames\\cam0\\";
String filePath1 = "E:\\Chris's stuff\\datalogs\\saveFrames\\cam1\\";
String filePath2 = "E:\\Chris's stuff\\datalogs\\saveFrames\\cam2\\";
String Time;


Capture cam0;
Capture cam1;
//Capture cam2;
ControlP5 cp5;
Serial arduinoPort;
int pan = 10;
int tilt = 10;
int lftarm = 10;
int rghtarm = 10;
int lfthand = 10;
int rghthand = 10;
int torso = 10;
Serial port;

void setup() {

arduinoPort = new Serial(this, "COM5", 57600);//change this to whichever port your arduino is on
size(1280,640);
frameRate(fps);
cp5 = new ControlP5(this);

//add slider horizontal for pan
cp5.addSlider("pan")
.setPosition(0,480)

.setSize(300,30)
//.setPosition(0,140)
//.setSize(100,30)
.setRange(0,180)
.setValue(158)
;

//add a horizontal slider for tilt

cp5.addSlider("tilt")
// .setBroadcast(false)
.setPosition(780,490)
.setSize(30,130)
// .setBroadcast(true)
.setRange(0,180)
.setValue(158)
;

cp5.addSlider("lftarm")
.setPosition(0,540)
.setSize(200,30)
.setRange(0,180)
.setValue(158)
;
cp5.addSlider("rghtarm")
.setPosition(400,540)
.setSize(200,30)
.setRange(0,180)
.setValue(158)
;
cp5.addSlider("lfthand")
.setPosition(0,580)
.setSize(200,30)
.setRange(0,180)
.setValue(158)
;
cp5.addSlider("rghthand")
.setPosition(400,580)
.setSize(200,30)
.setRange(0,180)
.setValue(158)
;
cp5.addSlider("torso")
.setPosition(900,580)
.setSize(200,30)
.setRange(0,180)
.setValue(158)
;


String[] cameras = Capture.list();

for (int i = 0; i < cameras.length; i++) {
print("camera " + i + ": ");
println(cameras[i]);
}

// The camera can be initialized directly using an
// element from the array returned by list():

try {
cam0 = new Capture(this, cameras[1]);
cam0.start();

cam1 = new Capture(this, cameras[5]);
cam1.start();

//cam2 = new Capture(this, cameras[21]);
// cam2.start();
}

catch (Exception e) {
println("Error attaching camera(s): " + e.getMessage());
exit();
}

}

void draw() {

cam0.read();
arduinoPort.write("a"+pan);
arduinoPort.write("b"+tilt);

arduinoPort.write("c"+lftarm);
arduinoPort.write("d"+rghtarm);
arduinoPort.write("e"+lfthand);
arduinoPort.write("f"+rghthand);
arduinoPort.write("g"+torso);




set(642, 0, cam0);
setTimestamp();
if(saveFrames) { saveFrame( filePath0 + fileName + "_cam0_" + Time + "_F##" + fileType ); }

cam1.read();
set(0, 0, cam1);
setTimestamp();
if(saveFrames) { saveFrame( filePath0 + fileName + "_cam1_" + Time + "_F##" + fileType ); }

//cam2.read();
//set(0, 241, cam2);
//if(saveFrames) { saveFrame( filePath2 + fileName + "_cam2_" + Time + "_F##" + fileType ); }

}


// read actually date & time, create Variable
void setTimestamp() {
String Year, Month, Day, Hour, Minute, Second;
Year = nf( year(), 4 );
Month = nf( month(), 2 );
Day = nf( day(), 2 );
Hour = nf( hour(), 2 );
Minute = nf( minute(), 2 );
Second = nf( second(), 2 );
Time = Year + Month + Day + "_T" + Hour + Minute + Second;
}




https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/11219329_10153026048008768_1360352962091331936_n.j pg?oh=4cf6ad4f438a1de478045cfdee9a4d34&oe=55FFB926&__gda__=1438756979_d1f9929597f1fb08998d1dd2dcd8f6d chttps://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-9/11151034_10153026048153768_5165527592637880202_n.j pg?oh=b486b023ffded0007ccb0d065afae4d7&oe=55C054B1&__gda__=1443456764_10d01f3fb8d5c130b02a59d89f9da39 ehttps://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-9/11259127_10153026048218768_6899775806960492236_n.j pg?oh=30cb20cbafb31fbe79bb50588e635596&oe=55CD0E87&__gda__=1440107978_e69b050cf46ec0c0e0626c33afecc5d 0https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-xpf1/v/t1.0-9/11011934_10153026047963768_1962311308739832708_n.j pg?oh=0d186392a2e76aab07c870f3007779f2&oe=55CFC569&__gda__=1438720790_39ccae35b4ad74d240aa9d47d6561c4 9

jwatte
05-15-2015, 10:46 PM
That's one big robot!

It sounds to me like what you really want to know is how to debug a problem like this, and how to make it as easy as possible for others on a forum to help you figure it out.

The first thing you have to do is get some kind of signal out of each end when it gets "something."
Then, you can write a sketch for the Arduino that, say, sets a LED whenever it receives anything on the serial port.
Make the simplest possible sketch that can do this, and try it. If it gets the LED, then you know that some kind of communication works.
If you don't get the LED, then the problem is in communication itself, or in the setup of the device. (Baud rate, channels, pairing, ... -- depending on how the devices work.)

Keep adding one thing at a time, to prove or disprove whatever your current theory of the problem is.
It's methodical engineering work, working on deduction, and it's something most engineers take a long time to develop.

If you want to cheat a little bit (and who doesn't? :-) then you can get yourself some kind of tool that displays signals directly, without having to change the sketches.
Such tools used by electrical engineers include an oscilloscope (typically a "digital storage scope" like a Rigol DS1052E for the hobbyist, or HP or Tektronix scopes for pros.) Another such tool includes a logic analyzer. A good, affordable logic analyzer you can plug into your computer is the Saleae Logic line. The Logic 4 is $99 and even has one channel of analog in (which can double as a kind of oscilloscope in a pinch.)
Note that most other USB plug-in scopes/analyzers in affordable price ranges are terrible. The Saleae is a wonderful exception to that general rule!

The nice thing with these tools is that they let you actually see the electrical signals. This will let you diagnose all kinds of problems, from "is the baud rate right?" to "are bits even coming out the serial port?" to "is there interference on the power bus because of motors?" On the other hand, you need to spend some time just learning to use the tools. If you're going to be doing projects in the future, this is generally a good investment.

Regarding your code, I have no idea what could be wrong with it, because it's too much for me to read without any more context (or, too much to read on a web forum, in general.)
The best thing you can do is break down the problem in sub-problems -- what do you *know* works, and what do you just *think* works? And then write a very small test that verifies what you know.
Once you've done that, you will find the very-small piece that doesn't work the way you expect, and can ask about that, which will make it easier for other people to help you get past that hurdle.

BrEtT
05-16-2015, 09:02 AM
Hi thanks for the information, an oscilloscope is definitely on my shopping list.i have managed to get the device working threw processing to the arduino. Lit up an led moved 2 servo (case 'A' : if(panpos > 0) {panpos -=1;}; break;).Im pretty sure my problem is the arduino will only read 1 digit, so i need to some how take this (arduinoPort.write("a"+pan); ) and break it up into 4 port.writes, maybe 5 for an end of line character,arduino side would have to read 4-5 times then do something.

KurtEck
05-16-2015, 10:52 AM
I don't know processing, so could be wrong, but
What characters are generated with: arduinoPort.write("a"+pan);
Assuming that pan has the value of -1, does it generate 4 bytes: a - 1 <cr>

If so I wonder if it is simply your Arduino program that appears like it is expecting that the number will have been sent first. That is you have a case 0-9 to build the number and when the a comes, it uses the currently built number...

So two choices, change the arduino code to expect number after command, or:
try arduinoPort.write(pan+"a");

Kurt

jwatte
05-16-2015, 11:05 AM
arduinoPort.write("a"+pan);


If this is literally the code you're writing, then it won't do what you think.
The type of "a" is pointer-to-const-char.
The result of "operator+()" on "pointer-to-const-char" and "integral value" is "pointer offset."
Thus, the code you wrote will be the same as this:


char const *ptr = "a";
ptr += pan;
arduinoPort.write(ptr);


Unless "pan" has the value 0, or 1, the result will be undefined, as the pointer will be offset to point to some other part of memory where some other data may or may not live.

When dealing with strings, I find that sprintf() is pretty helpful. Or, if you want to go all C++, std::stringstream.


char buf[14];
sprintf(buf, "a%d", pan);
arduinoPort.write(buf);

BrEtT
05-17-2015, 06:56 AM
Thank you all for your valuable information,i have ended up changing the arduino code, to a ServoFirmata found in the arduino examples.On the Processing 2 side i found an example of the ControlP5 library and have changed the code now to(arduino.analogWrite(9, pan);).I will still attempt the other code,but at moment this will work, and give me some time to start working out the wireless cameras. Once again thanks for help i was really at a dead end.