Shop OBEX P1 Docs P2 Docs Learn Events
Prop floats and integers to RPi to txt file — Parallax Forums

Prop floats and integers to RPi to txt file

JonathanJonathan Posts: 1,023
edited 2015-01-15 15:06 in Propeller 1
Hi All,

I have gotten my RPi up and running and talking to my Prop via a pair of xbees. I can send a series of ASCII bytes from the pi to the prop and receive a series of ASCII bytes back that contain a temperature reading from a DS18B20 that the prop is reading. Since the DS object returns a floating point value for the I have been rounding it into an integer (f.fround) then using a DIG command converting it to ASCII bytes. I am using a small python program (my first!) that I wrote to query the prop, get the data and then both display it and write it to a file. Ultimately this file will be read and published to a website via the Pi so I can check out things like my hot tub temp. at work. Very important stuff!

Is there a way to send floats (and integers for that matter) without converting it to ASCII? I have been reading up Python all day but can't seem to figure it out. There surely must be a way.

I'm also trying to figure out how to break down a string into a comma delimited array and writing that to a file. I can write the string in but need to insert the commas. I think the bytearray function is what I need for this.

Thanks for any help or examples of similar projects!

Jonathan

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-01-15 14:23
    Jonathan wrote: »
    Is there a way to send floats (and integers for that matter) without converting it to ASCII? I have been reading up Python all day but can't seem to figure it out. There surely must be a way.

    There is a way but it's messy. If you're not using ASCII characters then you can't have control characters since what you think is a control character could be a valid data point.

    You either need to always send the same number of bytes with each transmission or indicate how many bytes are being sent in the packet's header. There are a variety of protocols which do this. The communication protocol used by Dynamixel servos have a field indicating the message size. Modbus also has the number of bytes (IIRC, it could be the number of words) as part of the packet header.

    While dealing with raw data can be a pain, it's a good thing to know how to do. Sending data as ASCII characters can easily double the bytes sent compared with sending raw data.

    The hassle of converting data to and from ASCII characters is minor compared with the hassle of sending raw data values. I only bother with raw data when sending ASCII characters is too slow or when required by the devices with which I'm interfacing.
  • JonathanJonathan Posts: 1,023
    edited 2015-01-15 14:39
    Duane,

    I can stick to ASCII I guess. I won't be sending huge gobs of data and the system is not time sensitive at all. I have a number of prop based controllers (garden timer, rural well and water system, hot tub) that I am tying together with xbees and using the Pi to query the subsystems and then publish to the web.

    I'll keep pounding on figuiring out the comma delimited ASCII file writing. I have the correct data in the file, just need to figure out how to insert the commas. Also how to take a float and convert it to an integer while keeping what is to the right of the decimal point.

    Thanks!

    Jonathan
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-01-15 14:46
    Jonathan wrote: »
    I'll keep pounding on figuiring out the comma delimited ASCII file writing. I have the correct data in the file, just need to figure out how to insert the commas. Also how to take a float and convert it to an integer while keeping what is to the right of the decimal point.

    The object "StrFmt" I mentioned earlier will do both of these things.

    There are several ways to deal with the floating point number. One way would be to multiply it by an appropriate value prior to rounding the number. If, for example, you multiplied the number by 1000, you could then use the "decf" (IIRC) "fdec" method and indicate the decimal position as "3" in the appropriate parameter (which is the "dp" parameter).

    The object also makes it easy to add a character to a string. IIRC, It's the "ch" method.

    At least this how to do it in Spin. I don't know Python well enough to help.

    Edit: I just checked the "StrFmt.spin" object and made a couple of corrections.
  • JonathanJonathan Posts: 1,023
    edited 2015-01-15 14:52
    Duane,

    Great! Very helpful. And duh, of course multiply it! I'll go check out the StrFmt object. I could of course add the commas on the prop side, but that would mean even more bytes sent.

    Jonathan
  • ElectrodudeElectrodude Posts: 1,658
    edited 2015-01-15 15:06
    If you really wanted to, you could abuse the fact that there are 8 million possible values that are all NaN (and that probably won't show up in real data). I almost did that, until I realized that XBees had API packet mode and that I could just use separate packets instead of control characters. The packet format I used was a letter to specify what the data was followed by a bunch of 4 byte floats in binary. If you aren't already, I would definitely recommend using API mode instead of AT (transparent) mode. Packets are incredibly useful especially if any data is lost - it's trivial to resynchronize to the data stream - just wait for the next packet, as opposed to guessing whether or not what could be a control character is actually part of a float or what to do when you think you have valid data but then find it's also valid when shifted over a byte (and in that case means something completely different)

    There is a libxbee that should work on your Pi (it works great on my x86 linux and it's open source so I don't see why it wouldn't work) and a Spin library for the propeller that can both do either AT or API mode.

    libxbee
    Spin XBee driver
    my version of the Spin XBee driver that can be used with variants of 4PortFullDuplexSerial
Sign In or Register to comment.