Show Posts
Pages: [1] 2
1  Forum 2005-2010 (read only) / Troubleshooting / PWM Sync Between Ports on: January 23, 2010, 12:06:41 pm
I'm noticing some strange behavior with my PWM code.  I'm writing the same value to two pins, but one pin seems to always lag the other by several updates.  Here's the related code section:

Code:
while(blueVal > blueMax) {
  waitMinutes(1);
  blueVal -= ((255-blueMax)/20);
  analogWrite(blueLeft, blueVal);
  analogWrite(blueRight, blueVal);
 }

2  Forum 2005-2010 (read only) / Syntax & Programs / Re: Sending a data structure over serial on: June 05, 2009, 04:37:12 pm
I was making my edits in the wrong spot.  Everything is int16_t.  I had an extra item in the structure.

I am noticing that there is a "2570" showing up after I send my message.  Still trying to track that down.  Ideas?

The println at the end is the only way I could get the data to actually be received on the other end.  Suggestions there?

Here's the code:
Code:
typedef struct {
  int8_t id;
  int16_t messageCount;
  int16_t time;
} __attribute__((__packed__))packet_header_t;

typedef struct {
  packet_header_t header;
  int16_t rotationRate;
  int16_t therm1;
  int16_t therm2;
  int16_t heading;
  //uint32_t pressure;
  int16_t airTemp;
  int16_t checksum;
} __attribute__((__packed__))data_packet_t;

data_packet_t dp;

void setup() {
  Serial.begin(115200);
  dp.header.id = 99;
  dp.header.messageCount = 0;
  dp.header.time = 1;
  dp.rotationRate = 2;
  dp.therm1 = 3;
  dp.therm2 = 4;
  dp.heading = 5;
  dp.airTemp = 6;
  dp.checksum = 7;
}

void loop() {
  unsigned short checkSum;

  unsigned long uBufSize = sizeof(data_packet_t);
  char pBuffer[uBufSize];

  memcpy(pBuffer, &dp, uBufSize);
  for(int i = 0; i<uBufSize;i++) {
    Serial.print(pBuffer[i]);
  }
  Serial.println();

}
3  Forum 2005-2010 (read only) / Syntax & Programs / Re: Sending a data structure over serial on: June 05, 2009, 03:46:18 pm
Woops... figured it out.

Never mind.
4  Forum 2005-2010 (read only) / Syntax & Programs / Re: Sending a data structure over serial on: June 05, 2009, 03:39:59 pm
Here's a new one....

I figured out how to get the data structure to send.  I changed everything to be uint16_t on both sides.  I'm seeing two extra '0' come up in the data stream somewhere in the middle.  Ideas?

Code:
typedef struct {
  int messageCount;
  int time;
} __attribute__((__packed__))packet_header_t;

typedef struct {
  packet_header_t header;
  int16_t rotationRate;
  int16_t therm1;
  int16_t therm2;
  int16_t heading;
  //uint32_t pressure;
  int16_t airTemp;
  int16_t checksum;
} __attribute__((__packed__)) data_packet_t;
5  Forum 2005-2010 (read only) / Syntax & Programs / Re: Sending a data structure over serial on: June 04, 2009, 09:51:37 am
Excellent point.  Didn't think about that.  One side is an Arduino, the other is a Gumstix.  I'll run a check to verify that they are the same size.

Thanks.
6  Forum 2005-2010 (read only) / Syntax & Programs / Re: Sending a data structure over serial on: June 04, 2009, 09:31:13 am
Hey AWOL,

The data structure is defined on both sides of the link.  I guess I can just do a memcpy of the structure onto a char array.  That should work right?
7  Forum 2005-2010 (read only) / Syntax & Programs / Sending a data structure over serial on: June 04, 2009, 09:00:13 am
I'm looking to send a data structure over the serial port.  Is there a way to use Serial.print() for this or is there a better way to send it?
8  Forum 2005-2010 (read only) / Syntax & Programs / Re: Strange behavior with serial.read() on: June 10, 2009, 07:48:57 pm
Here's the current version.  It looks for the message ID to sync up to.  Then it reads the whole message in.  

Is there a way to speed this read process up?

Code:
if(Serial.available()) {
    int8_t val;
    val = Serial.read();
    if(val == 99) {
      buffer[0] = val;
      int i = 1;
      while(i<8) {
        val = Serial.read();
        if(val!= -1) {
          buffer[i] = val;
          i++;  
        }
      }
      memcpy(&controlPacket, buffer, sizeof(controlPacket));
    }
  }
9  Forum 2005-2010 (read only) / Syntax & Programs / Re: Strange behavior with serial.read() on: June 10, 2009, 08:38:51 am
It could be a problem with the data source.  I did manage to get some code to work.  Here's what actually works to read the data in.

There are still some issues that I'm trying to work out.  Like sometimes I get it at a regular interval, others it comes in a flurry.

Note: The val == 99 part is an id field it is using to sync up with.
Code:
 if(Serial.available()) {

  int8_t val;
  val = Serial.read();
  if(val!=-1 && (idx > 0 || val==99)){
    buffer[idx++] = val;
  }
  if(idx >= sizeof(controlPacket)){
      idx = 0;
      memcpy(&controlPacket, buffer, sizeof(controlPacket));
      //radio.print("Flap: " );
      radio.println(controlPacket.flapAngle);
  }

10  Forum 2005-2010 (read only) / Syntax & Programs / Re: Strange behavior with serial.read() on: June 09, 2009, 08:03:46 am
Hey Everyone,

Thanks for the replies.  I gave all of them a shot.  No better though.

I went so far as to just do this in my loop:

Code:
 if(Serial.available()) {
    stop = false;
    while(Serial.available() !=0) {
      radio.println(Serial.read(), HEX);
    }

I still see the same thing.  First set of bytes is fine.  Next set gets every other.
11  Forum 2005-2010 (read only) / Syntax & Programs / Re: Strange behavior with serial.read() on: June 08, 2009, 05:20:14 pm
Here's a little data... looks like I get the first copy fine and then the next one, I only get every other byte.  (Should be 9 bytes)

Code:
Data
Got the id
3C
0
4B
0
33
A
28
0
Size: 1
Flap Angle: 51
Data
Got the id
3C
4B
34
21
9
FFFFFFFF
12  Forum 2005-2010 (read only) / Syntax & Programs / Strange behavior with serial.read() on: June 08, 2009, 05:13:31 pm
I'm having some weird behavior here.  I think I'm missing something obvious.  

I am sending a data structure that was copied onto a char array over the serial link from a Gumstix to the Arduino.  (This works in the other direction)  I get the first copy of the structure fine, but subsequent ones aren't right.

Structure:
Code:

typedef struct {
  int8_t id;
  int16_t messageCount;
  int16_t time;
} packet_header_t;

typedef struct {
  packet_header_t header;
  int8_t flapAngle;
  int8_t motorSpeed;
  uint16_t checksum;
} control_packet_t;

Loop:

Code:

void loop() {
  char temp;
  if(Serial.available()) {
    radio.println("Data");
    int8_t id = Serial.read();

    if(id == 99) {
      radio.println("Got the id");
      rec = 1;
      while(rec < 8) {
        temp = Serial.read();
        if(temp == -1) rec = 8;
        buffer[rec] = temp;
        rec++;
      }
      radio.print("Size: ");
      radio.println(sizeof(temp));
      memcpy(&controlPacket, buffer, sizeof(buffer));
      radio.print("Flap Angle: ");
      radio.println(controlPacket.flapAngle);
      Serial.read();
      rec = 0;
    }
  }

13  Forum 2005-2010 (read only) / Syntax & Programs / Re: detecting the end of a count on: June 11, 2009, 09:48:56 am
How is the data coming into the Arduino?  Like mem said, if there was a unique header or terminator on each type, you could key off of that.
14  Forum 2005-2010 (read only) / Syntax & Programs / Re: REQ: Serial.available() < 2 as single value on: June 10, 2009, 02:15:27 pm
Yep AWOL.  I put a note at the end that said it assumes there is data for both available.
15  Forum 2005-2010 (read only) / Syntax & Programs / Re: REQ: Serial.available() < 2 as single value on: June 10, 2009, 02:00:35 pm
You can map each reading onto a char array.  Then just read the char array.

Code:

char input[2];

if(Serial.available()) {
   input[0] = Serial.read();
   input[1] = Serial.read();
}

Serial.print(input);

This does ignore the fact that there might not be data there for both reads.
Pages: [1] 2