Let's Make Robots!

First Serial Comunication!!

Chris the Carpenter's picture

Alright guys, I have my first serial conection comunication going!! Picaxe to Processing via serial/USB. I don't have this up to youtube, instead I did a quick post to Phreadz. Here is the link:

http://phreadz.com/v/2Q0FFVH6IGAI/

***Update****

Just like Voodoo, I got some code working as well. The first video has the picaxe spitting out 4 bytes (randomly picked by me when I wrote the code) as I hit one of the 3 buttons. Processing then takes these numbers and draws 1 of 3 lines.

Having this working, I moved on to changing data. I grabbed walters remote and used the joystick to send 2 bytes coresponding to the cordinates I wanted the line to end on -the start cordinates were always 0,0. Although I was using the remote, it was hardwired and used sertxd and serrxd commands. (this is the second video)

I will attach the code soon --for now, I am off to go fly planes with my boys.

 Code: (second video, processing side)

import processing.serial.*;

int bgcolor;                 // Background color
int fgcolor;                 // Fill color
Serial myPort;                       // The serial port
int[] serialInArray = new int[4];    // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
int xpos, ypos;   
int expos, eypos;             // Starting position of the ball
boolean firstContact = false;        // Whether we've heard from the microcontroller

void setup() {
  size(400, 400);  // Stage size
  noStroke();      // No border on the next thing drawn

  // Set the starting position of the ball (middle of the stage)
  xpos = width/2;
  ypos = height/2;
  expos = width/2;
  eypos = height/2;

  // Print a list of the serial ports, for debugging purposes:
  println(Serial.list());

  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, "COM15", 4800);
}

void draw() {
  background(200);
  fill(fgcolor);
  stroke (0);
  strokeWeight(4);
  // Draw the shape
  line(xpos, ypos, expos, eypos);
}

void serialEvent(Serial myPort) {
  // read a byte from the serial port:
  int inByte = myPort.read();
  // if this is the first byte received, and it's an A,
  // clear the serial buffer and note that you've
  // had first contact from the microcontroller.
  // Otherwise, add the incoming byte to the array:
  if (firstContact == false) {
    if (inByte == 'A') {
      println (inByte);
      myPort.clear();          // clear the serial port buffer
      firstContact = true;     // you've had first contact from the microcontroller
      myPort.write('A');       // ask for more
    }
  }
  else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    serialCount++;

    // If we have 3 bytes:
    if (serialCount > 3 ) {
      xpos = serialInArray[0];
      ypos = serialInArray[1];
      expos = serialInArray[2];
      eypos = serialInArray[3];
     
      // print the values (for debugging purposes only):
      println(xpos + "\t" + ypos + "\t" + expos +"\t" + eypos);

      // Send a capital A to request new sensor readings:
      delay (100);
      myPort.write('A');
      // Reset serialCount:
      serialCount = 0;
    }
  }
}

 

Code: (for the second video)


pause 1000
sertxd (65)

main:
serrxd b0
if b0 > 0 then main2
goto main


main2:
readadc 0,b3
readadc 1,b4
let b3=b3
let b4=b4
let b1=0
let b2=0
sertxd (b1)
pause 10
sertxd (b2)
pause 10
sertxd (b4)
pause 10
sertxd (b3)
goto main


Embedded video: 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
CaptainTuna's picture
I think all this progress should be saved as a Tip!
voodoobot's picture

Hey CTC, this was very helpful, I managed to get my code working ang gathering 4 bytes of data, with a start and stop byte. drawing shouldn't be an issue at this point.

 Edit:  Hey Chris, I got the code working(even better than before) this eve. The processing code takes in the start and stop byte, reads the data. As far as using this for mapping I think it'll work, but i did find an issue with what I would use the code for and need to figure out how to resolve it. Basically things get out of sync if I start droping bytes(block the ir transmission). The bytes come in correctly  but they don't get displayed correctly. I think it's with the processing code, but I got a bit complex in the data transmission code(you'll see when I post it). It's making my brain hurt so I'm going to take a break....

 I'm finding a lot of the syncing issues have to do with very subtle ms delays...like around 20ms....

This is where I think the issue is in the code. This is my modified version of the  serial event function. This is making my brain hurt as well....

 void serialEvent(Serial myPort) {
  // read a byte from the serial port:
  int inByte = myPort.read();
  // look for the start byte
  if (firstContact == false) {
    if (inByte == 'S') {
     println(inByte);
      myPort.clear();          // clear the serial port buffer
    firstContact = true;  
      }
    }
  else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
  println(inByte);
    if (inByte == 'E') {
   //look for the end byte

   // myPort.clear();          // clear the serial port buffer
    firstContact = false;  }
    else {
      serialCount++;
      // If we have 4 bytes:
        if (serialCount > 3 ) {
          xpos = serialInArray[0];
          ypos = serialInArray[1];
          upos = serialInArray[2];
          vpos = serialInArray[3];
 
          // Reset serialCount:
          serialCount = 0;
          }
    }    
   }

}

Chris the Carpenter's picture
why did I write this responce before I updated the post?
rik's picture

Got my "hello_serial" all Processed now.

import processing.serial.*;
 
Serial PicPort;

void setup() {
   //                 parent comport           baud  parity databits stopbits
   PicPort = new Serial(this, Serial.list()[0], 4800, 'N',   8,       1.0);
}

void draw() {
   while (PicPort.available() > 0) {
     int PicSaid = PicPort.read();
     println(PicSaid);
   }
}
 
 
This will read in values that my picaxe sends me over the serial (programming) cable. Text is also println'd as values.

Goodnight

voodoobot's picture
Thanks rik and have a good night!
Calculon320's picture

we're proud of you and you're awesome!

The language is called "processing"? The serial is recieved by the OS on the laptop?

voodoobot's picture

Alrighty, ctc, got the rig working. The setup is the following:

<ir beacon> transmitting byte sequence from 1-9

<relay station> picaxe 8m with ir receiver showing correct data in the debug window.

Transmitting the byte sequence received from ir beacon via bt to my desktop running the processing code.

I'll post  source code  for all three components(irb, picaxe relay station, processing code) in a bit. how can I post the source as files?

edit: nm, I added it to a blog post of my own that links to this post.

Just a note, the shot below was active and transmitting the number sequence(from the beacon) out through the bt(note green connect led on the bt device)  :D

 pa8M_bluetooth.jpg

 

fritsl's picture

you so funny :D

CTC's the party!

jip's picture

Hi Chris,

This is awesome! Congratulations on you venturing into big boy programming land :-). Still what impresses me the most is your skill in creating great and enthusiastic broadcasts - keep'em coming!

Great work!