Shop OBEX P1 Docs P2 Docs Learn Events
Need help receiving decimal values using fdserial_rxChar(fdserial *term) — Parallax Forums

Need help receiving decimal values using fdserial_rxChar(fdserial *term)

TCIIITCIII Posts: 81
edited 2022-06-29 21:35 in C/C++

Hi All,

I have an Arduino Leonardo sending two decimal speed values (64) to my Propeller Activity Board WX as a test using the follow cpp code:

#include <SoftwareSerial.h>
.
.
SoftwareSerial prop(2,3);
#define ARLO_FAST 64
.
.
.
prop.begin(38400);
int left, right;

left = ARLO_FAST;
right = ARLO_FAST;
prop.print(left, DEC);
prop.print(right, DEC);

I have verified that the Leonardo is sending the "64" decimal ASCII value by using a serial to USB converter and viewing the Leonardo serial output on the SimpleIDE terminal.

My Propeller C code looks like this:

#include "simpletools.h"
#include "arlodrive.h"
#include "fdserial.h"

/ ------ Global Variables and Objects ------
int leftSpeed;
int rightSpeed;
fdserial *fdser5_4;

// ------ Main Program ------
int main() {
  fdser5_4 = fdserial_open(5, 4, 0b0000, 38400);
  // Pixy2_Line_Following

  while(1) {
    leftSpeed = fdserial_rxChar(fdser5_4);
    rightSpeed = fdserial_rxChar(fdser5_4);
    print(("Left = "));
    print("%d\r", leftSpeed);
    print(("Right = "));
    print("%d\r", rightSpeed);
    //drive_speed(leftSpeed, rightSpeed);
  }
}  

The problem I am having is that even though the decimal value 64 is being transmitted by the Leonardo over the serial connection with the Propeller, I am seeing the decimal values 52 and 54 being displayed on the SimpleIDE terminal.

I have even tried using serial.h with the same results!

What is my Propeller code missing that is causing the decimal value of "64" to be received as either "52" or "54"?
Is it a fdserial receiver buffer issue?

Any help would be appreciated to help move my project along.

Regards,
TCIII

Comments

  • AribaAriba Posts: 2,682

    With prop.print(left, DEC); you send the value left as two ASCII characters '6' and '4' . The ASCII values for '6' and '4' are 54 and 52, that is what you receive in the Spin code (you only read single characters).
    You can send the value as a single character with prop.write(left) on the Arduino side. This limits the range of the speed value to 0..255.
    Or you receive the decimal values which are built from several ASCII characters on the Propeller side. This is a bit more complicated but allows to add some synchronization characters to distinguish between left and right value.

    Andy

  • @Ariba,

    Thanks for the response, much appreciated.

    I eventually figured out that I needed to use prop.write and not prop.print as you have pointed out.

    Regards,
    TCIII

  • TCIIITCIII Posts: 81
    edited 2022-06-30 23:21

    Hi All,

    Now that I have gotten the Leonardo to Propeller Activity Board serial communication issue put to bed, I still have a problem, but with the Propeller code.

    Since I don't need bidirectional communication I have switched from fdserial.h to serial.h and have changed drive_speed(int left, int right) to drive_rampStep(int left, int right) which is better suited to varying speed values in a loop.
    The problem I am having now is that only the right wheel turns at the commanded speed (64) while the left wheel stays stationary even though the print statements, in the Terminal, for the leftSpeed and the rightSpeed both show a speed value of 64.
    It seems that the leftSpeed value is not being used in the drive_rampStep(leftSpeed, rightSpeed) or am I trying to do something that the drive_rampStep(leftSpeed, rightSpeed) function is not capable of in the while(1) loop?
    Additionally the "arlodrive.h" header library that I am using is a specialized version for an Arlo that uses HB25s in place of the DHB10 Motor Controller. I can use drive_speed(int left, int right), drive_goto(int left, int right), and drive_rampStep(int left, int right) in other programs and they function as expected.

    #include "simpletools.h"
    #include "arlodrive.h"
    #include "serial.h"
    
    / ------ Global Variables and Objects ------
    int leftSpeed;
    int rightSpeed;
    serial *ser5_4;
    
    // ------ Main Program ------
    int main() {
      ser5_4 = serial_open(5, 4, 0b0000, 38400);
      // Pixy2_Line_Following
    
      while(1) {
        leftSpeed = serial_rxChar(ser5_4);
        rightSpeed = serial_rxChar(ser5_4);
        print(("Left = "));
        print("%d\r", leftSpeed);
        print(("Right = "));
        print("%d\r", rightSpeed);
        drive_rampStep(leftSpeed, rightSpeed);
      }
    }  
    

    Regards,
    TCIII

  • The arlo code shows that you can turn on debug so that it prints out messages as it goes through the code.

    At the top of the arlodrive.h there these three lines:

    //#define ARLO_DEBUG
    //#define XBEE_TERMINAL 
    //#define HALF_DUPLEX
    

    If you remove the two "//" from the front of the first line you should get debug data.

    The code is using pins 12 and 13 which should work. Did you try reversing the wires to see if the other motor works as well.

    Mike

  • TCIIITCIII Posts: 81
    edited 2022-07-01 15:27

    @iseries,

    Thanks for the response, much appreciated.

    Unfortunately my modified arlodrive.h does not contain the //#define ARLO_DEBUG. Instead it has //#define interactive_development_mode which I believe has the same function.

    It turns out that something had happened to my original Arlo motor/encoder calibration values in the eeprom. A recalibration of the Arlo motor/encoders solved the issue.

    Now the Arduino Leonardo receives line tracking data ( x offset distance from the camera center of view x value) from the Pixycam2, converts it to left and right steering speed values, and transmits those two values over a serial link to the Propeller Activity Board running the C code in post #4 above.

    I have a piece of white paper with a 1/8" black line drawn down the center to simulate a line path and if I move the line to the left of the camera FOV center, the right wheel speeds up and the left wheel slows down. Moving the line to the right of the camera FOV center, the right wheel slows down and the left wheel speeds up.

    Regards,
    TCIII

  • Duane DegnDuane Degn Posts: 10,588
    edited 2022-07-06 03:04

    @TCIII said:
    What is my Propeller code missing that is causing the decimal value of "64" to be received as either "52" or "54"?

    The character "6" is ASCII code 54. The character "4" is ASCII code 52.

    To convert the ASCII characters to a numeric value you can subtract the character "0" from each digit. "0" is ASCII code 48. You multiply the previous values by ten and add the value for the new character to build up the entire number.

    I'm pretty sure this functionality is built into one of the libraries you are using but it's been too long since I've used C on the Propeller for me to remember the syntax off the top of my head.

    Edit: I think I just repeated what Andy said above. Sorry Andy. I hadn't read all the replies before commenting.

    Edit again: Sorry all. I replied to the wrong comment.

Sign In or Register to comment.