Serial telemetry streaming via LUA or BT spoof. Fast enough?

Discussion on the Lua Scripting capabilities for RaceCapture/Pro. Also see the <a href="http://autosportlabs.net/RaceCapturePro_Lua_Scripting">Lua Scripting Guide</a>

Moderators: JeffC, stieg

zechdz
Posts: 44
Joined: Sun Nov 08, 2015 3:04 pm

Serial telemetry streaming via LUA or BT spoof. Fast enough?

Post by zechdz »

I posted in telemetry about wifi access on the RCP, and that I had purchased a chip. However that chip requires a lot of embedded (c-like) setup and I don't have the time to go through all that :P. So... instead I found a really convenient chip, ESP8266, which runs NodeMCU allowing for control through Lua scripts for WiFi connections.

I wanted to make a serial interface between the RCP and this chip using LUA on both sides, allowing for some sharing of code, and was wondering if streaming through the LUA UART interface would allow for high quality streaming as is done with the telemetry/bt modules.

My second thought was to make this wifi module spoof the current BT module you guys have, and just directly hardcode things like host/port into the NodeMCU code, to act like the RCP app would. If doing this is better than using Lua, do you guys have any documentation on the BT interface so I could spoof that serial port connection?

I took a quick look through the firmware code, but haven't been able to piece it all together. Keep getting lost between whats a settings communication and whats a streaming communication in regards to BT. And if I had more time I would probably try to add my own firmware module for NodeMCU streaming and what not, but right now I'm trying to find the quickest/easiest way to get wifi so I can concentrate on riding.

So in summary:

1) is the Lua library (module? plugin?) quick enough/good enough to stream serial to my wifi module (including access point information before streaming). (note: also figured this path would allow me to use the auxiliary streaming port (or telemetry connection in the box) for my wifi module and the BT module is in the firmware, so could have an android phone and direct wifi setup at the same time)

2) if not, is there docs on how to spoof the BT module, so that RCP firmware thinks its communicating via bluetooth but instead is using my module.



And when I get this stuff working I will open source my code so others can see/use/help work out bugs. The ESP8266 is very cheap and easy to use, so i'm sure other people might be interested.
vroom

stieg
Posts: 100
Joined: Fri Dec 20, 2013 1:37 am
Location: Madison, WI
Contact:

Post by stieg »

The second approach sounds more realistic from my perspective. The BT device is treated as a simple NULL serial modem, so if your device is already setup to work the way you want and can handle a raw stream, then all you need to do is simply ignore the setup commands and listen for the JSON stream.

Check out https://github.com/autosportlabs/RaceCa ... luetooth.c to see how BT is setup (this is unreleased code... it is part of the next release and has undergone some cleanup).

Check out connectivityTask in https://github.com/autosportlabs/RaceCa ... vityTask.c to see how data is sent (hint, its just a RAW JSON stream after init).

Hope that helps.
Andrew Stiegmann (Stieg)
Principal Engineer
Autosport Labs Inc.

zechdz
Posts: 44
Joined: Sun Nov 08, 2015 3:04 pm

Post by zechdz »

Thank you for the reply stieg. So I am on step 1 of this project which is to stream fake data from the wifi module. I managed to get that all setup, do auth, and then send meta and a stream of data.

However the data showing up on the race-capture website is not right. Is there an order I should send the data in? I'm sending the data in the same order as the meta was sent, but it doesn't work. I also tried alphabetical and that didn't work either...


Meta:

Code: Select all

do send_msg&#58; &#123;"s"&#58;&#123;"meta"&#58;&#91;&#123;"ut"&#58;"G","nm"&#58;"AccelX","sr"&#58;25&#125;,&#123;"ut"&#58;"G","nm"&#58;"AccelY","sr"&#58;25&#125;,&#123;"ut"&#58;"G","nm"&#58;"AccelZ","sr"&#58;25&#125;,&#123;"ut"&#58;"Volts","nm"&#58;"Battery","sr"&#58;1&#125;,&#123;"ut"&#58;"Miles","nm"&#58;"Distance","sr"&#58;50&#125;,&#123;"ut"&#58;"Count","nm"&#58;"LapCount","sr"&#58;1&#125;,&#123;"ut"&#58;"Min","nm"&#58;"LapTime","sr"&#58;1&#125;,&#123;"ut"&#58;"Degrees","nm"&#58;"Latitude","sr"&#58;50&#125;,&#123;"ut"&#58;"Deg\/Sec","nm"&#58;"Longitude","sr"&#58;50&#125;,&#123;"ut"&#58;"Count","nm"&#58;"Sector","sr"&#58;1&#125;,&#123;"ut"&#58;"Min","nm"&#58;"SectorTime","sr"&#58;1&#125;,&#123;"ut"&#58;"MPH","nm"&#58;"Speed","sr"&#58;50&#125;,&#123;"ut"&#58;"","nm"&#58;"Time","sr"&#58;50&#125;,&#123;"ut"&#58;"Deg\/Sec","nm"&#58;"Yaw","sr"&#58;25&#125;&#93;&#125;&#125;

Data:

Code: Select all

do send_msg&#58; &#123;"s"&#58;&#123;"d"&#58;&#91;2.5,2.5,-2.5,5.1,0,0,0,39.745716841307,139.07335743824,0,0,11.929658833858,0,50,30&#93;&#125;&#125;

do send_msg&#58; &#123;"s"&#58;&#123;"d"&#58;&#91;2.5,2.5,-2.5,5.1,20,0,1,36.690151892458,144.0525280759,0,1,18.843932793869,1,50,30&#93;&#125;&#125;
do send_msg&#58;
vroom

zechdz
Posts: 44
Joined: Sun Nov 08, 2015 3:04 pm

Post by zechdz »

Here is some rough lua code following the logic of the RCP code found at:
https://github.com/autosportlabs/RaceCa ... nection.py

Code: Select all

function sendMeta&#40;&#41;
    local msg = &#123;s=&#123;meta=&#123;&#125;&#125;&#125;
    local meta = &#123;&#125;

    local count = 0
    for sortedk, sortedv in pairs&#40;channelmeta&#41; do
        for k, v in pairs&#40;sortedv&#41; do
            print&#40;"Channel name&#58; "..k&#41;
            table.insert&#40;meta, &#123;
                nm= v.name,
                ut= v.units,
                sr= v.sampleRate,
    --            min= v.min,
    --            max= v.max
            &#125;&#41;
            count=count+1
         end
     end

    msg.s.meta = meta
    sendMessage&#40;cjson.encode&#40;msg&#41;&#41;
end

Code: Select all

function sendSample&#40;count&#41;
     local msg = &#123;s=&#123;d=&#123;&#125;&#125;&#125;
     local bitmasks = &#123;&#125;
     local channelData = getFakeChannelData&#40;count&#41;
     local bitmasks_needed = math.max&#40;0, math.floor&#40;&#40;tablelength&#40;channelData&#41; - 1&#41; / 32&#41; + 1&#41;
     local channel_bit_position = 0
     local bitmask_index = 1
     local data = &#123;&#125;

    for x = 1, bitmasks_needed do
        table.insert&#40;bitmasks, 0&#41;
    end

    for sortedk, sortedv in pairs&#40;channelmeta&#41; do
        for k, v in pairs&#40;sortedv&#41; do
            if &#40;channel_bit_position > 31&#41; then
                bitmask_index = bitmask_index + 1
                channel_bit_position = 0
            end
    
            if &#40;channelData&#91;k&#93; ~= nil&#41; then
                bitmasks&#91;bitmask_index&#93; = bit.bor&#40;bitmasks&#91;bitmask_index&#93;,&#40;bit.lshift&#40;channel_bit_position,1&#41;&#41;&#41;
                table.insert&#40;data, channelData&#91;k&#93;&#41;
            end
    
            channel_bit_position = channel_bit_position + 1
         end
     end

    for index, bitmask in pairs&#40;bitmasks&#41; do
        table.insert&#40;data, bitmask&#41;
    end

    msg.s.d = data
    sendMessage&#40;cjson.encode&#40;msg&#41;&#41;
end
[/code]
vroom

stieg
Posts: 100
Joined: Fri Dec 20, 2013 1:37 am
Location: Madison, WI
Contact:

Post by stieg »

You are very close. I see two missing bits.

First, did you send the auth message? You need to auth a device against RCL before you can send data, else it will drop you. Its the first thing we do. Unfortunately this is something that the telemetry channel does. You may need to fake it with lua. Look at https://github.com/autosportlabs/RaceCa ... s/sim900.c for writeAuthJson for an example.

Second, looking at the raw JSON you are missing the "tick" field. This is a counter that increments with every sample sent. It lives in the `s` (for sample) object next to meta. Example is below:

Your JSON code:

Code: Select all

&#123;"s"&#58;&#123;"meta"&#58;&#91;&#123;"ut"&#58;"G","nm"&#58;"AccelX","sr"&#58;25&#125;,
Our JSON code:

Code: Select all

&#123;"s"&#58;&#123;"t"&#58;0,"meta"&#58;&#91;&#123;"nm"&#58;...
See that `t` object. That is what you are missing.

For examples I would recommend looking at the JSON samples we use for testing. These are kept up to date as we use them for our unite tests:

https://github.com/autosportlabs/RaceCa ... onse1.json
https://github.com/autosportlabs/RaceCa ... onse2.json
Andrew Stiegmann (Stieg)
Principal Engineer
Autosport Labs Inc.

zechdz
Posts: 44
Joined: Sun Nov 08, 2015 3:04 pm

Post by zechdz »

Yeah I did the auth part, just didnt include it in my post here. Then after auth i was ablento see live data on race-capture it just kept matching data to different meta. So my battery value would get distance values for example, even though I sent the meta and data in the same order.

I used some of the json samples, but I must've overlooked the tick key!

I'll give that a try.
vroom

zechdz
Posts: 44
Joined: Sun Nov 08, 2015 3:04 pm

Post by zechdz »

Very dumb miss

Code: Select all

bit.lshift&#40;1,channel_bit_position&#41;
                
vs

bit.lshift&#40;channel_bit_position,1&#41;
                
vroom

stieg
Posts: 100
Joined: Fri Dec 20, 2013 1:37 am
Location: Madison, WI
Contact:

Post by stieg »

Oops.... silly parameter orders...
Andrew Stiegmann (Stieg)
Principal Engineer
Autosport Labs Inc.

zechdz
Posts: 44
Joined: Sun Nov 08, 2015 3:04 pm

Post by zechdz »

Do you have any documents on the meaning of these values?

Code: Select all

&#123;nm="Interval",ut="ms",min=0,max=0,prec=0,sr=1&#125;,
&#123;nm="Utc",ut="ms",min=0,max=0,prec=0,sr=1&#125;,
&#123;nm="LapCount",ut="",min=0,max=0,prec=0,sr=10&#125;,
&#123;nm="LapTime",ut="Min",min=0.0,max=0.0,prec=4,sr=10&#125;,
&#123;nm="Sector",ut="",min=0,max=0,prec=0,sr=10&#125;,
&#123;nm="SectorTime",ut="Min",min=0.0,max=0.0,prec=4,sr=10&#125;,
&#123;nm="CurrentLap",ut="",min=0,max=0,prec=0,sr=10&#125;
1) What is Interval and UTC (I know what UTC is i just dont know what this data should mean, why would the RCP have access to a true UTC time... from gps?)
2) I'm not sure of the difference between CurrentLap and LapCount (is LapCount total lap count?)
3) I am also not sure how to use LapTime vs LapCount. (same as Sector vs SectorTime)

I tried this:

Code: Select all

LapCount=1,
LapTime=count/60,
but the lap time and lap count increased together with the value set in LapTime!



I also logged data of the weird lap count thing, but all the data disappeared in a day :(
https://www.race-capture.com/events/415 ... ing-logger
vroom

stieg
Posts: 100
Joined: Fri Dec 20, 2013 1:37 am
Location: Madison, WI
Contact:

Post by stieg »

{nm="Interval",ut="ms",min=0,max=0,prec=0,sr=1},
nm - name (12 char max)
ut - unit (7 char max)
min - Minimum value
max - maximum value
prec - Precision (used for floating point)
sr - Sample Rate (samples/second)
1) What is Interval and UTC (I know what UTC is i just dont know what this data should mean, why would the RCP have access to a true UTC time... from gps?)
internval is uptime in milliseconds
utc is utc time in milliseconds. Its time is retrieved from GPS. GPS is effectively an atomic clock.
2) I'm not sure of the difference between CurrentLap and LapCount (is LapCount total lap count?)
CurrentLap is what lap you are on. LapCount is how many you have completed.
3) I am also not sure how to use LapTime vs LapCount. (same as Sector vs SectorTime)
LapTime is how long the last lap was. LapCount is how many laps you have completed.
I also logged data of the weird lap count thing, but all the data disappeared in a day Sad
https://www.race-capture.com/events/415 ... ing-logger
That is... strange. Did you complete a single lap?
Andrew Stiegmann (Stieg)
Principal Engineer
Autosport Labs Inc.

zechdz
Posts: 44
Joined: Sun Nov 08, 2015 3:04 pm

Post by zechdz »

Thanks again for the quick response.

Nice to know about the max limits for name and units, whenever you guys get a chance document this stuff in github or api docs :-D. I mostly say this because I noticed the announcement of podium, and I'm sure transitioning my code to podium is going to have similar hurdles, if the api changes.

Okay so the values I streamed was this below:
linearly increasing Time, Distance, LapTime, SectorTime

Code: Select all

              AccelX=2.50,
              AccelY=2.50,
              AccelZ=-2.50,
              Battery=5.1,
              Distance=20*count,
              LapCount=1,
              LapTime=count/60,
              Latitude=35 + &#40;5*math.random&#40;&#41;&#41;,
              Longitude=136 + &#40;10*math.random&#40;&#41;&#41;,
              Sector=0,
              SectorTime=count/60,
              Speed=20*math.random&#40;&#41;,
              Time=count/60,
              Yaw=50,
From what you said about LapTime and CurrentLap I see what my problem is. I thought LapTime was the current lap time, but rather it is the last lap time. So I sent a new LapTime that increased at the same rate as Time (I wanted to simulate a long single lap for simplicity). But I never increased the LapCount. So it seems that live data thought i was increasing laps as I kept sending new LapTimes, but LapCount never increased so when you refresh the browser it sees that there was only ever LapCount=1 and only loads the first lap. This might be a bug on the server side, but its definitely a misuse of the API on my part.

I didn't know about CurrentLap at first because I based my data on what seems to be only a subset of the values I can use found at the link below. And since there were no API docs I just assumed LapTime had to be what CurrentLap is :-D
https://github.com/autosportlabs/RaceCa ... pledata.py
vroom

stieg
Posts: 100
Joined: Fri Dec 20, 2013 1:37 am
Location: Madison, WI
Contact:

Post by stieg »

Nice to know about the max limits for name and units, whenever you guys get a chance document this stuff in github or api docs Very Happy
Definitely. We still intend to cleanup the API at some point but yeah this has been a TODO for some time.
From what you said about LapTime and CurrentLap I see what my problem is. I thought LapTime was the current lap time, but rather it is the last lap time. So I sent a new LapTime that increased at the same rate as Time (I wanted to simulate a long single lap for simplicity). But I never increased the LapCount. So it seems that live data thought i was increasing laps as I kept sending new LapTimes, but LapCount never increased so when you refresh the browser it sees that there was only ever LapCount=1 and only loads the first lap. This might be a bug on the server side, but its definitely a misuse of the API on my part.
Yeah... the nomenclature is definitely confusing and could use some love.

RE the LapCount, RCL triggers a lap save when the LapCount changes. So to get laps to store, you need to complete the lap (ie increment LapCount). That will probably get you what you want :D.

Also... you are doing quite well considering 0 docs. Good work.
Andrew Stiegmann (Stieg)
Principal Engineer
Autosport Labs Inc.

zechdz
Posts: 44
Joined: Sun Nov 08, 2015 3:04 pm

Post by zechdz »

I got it to mock at least Lat/Lon and some crappy lap/time info going around Motegi using some mock data files I created from a session I did with my old logger (Aim solo)

Code so far: https://github.com/zkdzegede/nodemcu-rcp

My bike has been in the shop, hence working on this project with only mock data. But I get the bike (with RCP) back this week, so then I'll be changing my mock UART interface to a real one and trying it with real Lat/Lon values.

The rest of the values will also hopefully be trivial passing of information uart->json. If not I'll have to re-do the firmware to free up some more RAM haha. The nodemcu is so constrained
:x
vroom

stieg
Posts: 100
Joined: Fri Dec 20, 2013 1:37 am
Location: Madison, WI
Contact:

Post by stieg »

Killer. Let us know how it goes. Also feedback on motorcycle data would be great. Would love to know what low hanging fruit can be addressed to make it more useful for ya.
Andrew Stiegmann (Stieg)
Principal Engineer
Autosport Labs Inc.

zechdz
Posts: 44
Joined: Sun Nov 08, 2015 3:04 pm

Post by zechdz »

I still have a long road ahead to connect my analog sensors on the bike but i'll try to document it and let you guys know of anything that might help your product :).

I have however run into another hurdle. I commented here: https://github.com/autosportlabs/RaceCa ... issues/165

So what i was able to do:

1) Connect ESO8266 TX -> RX (yes to itself), and *could* send/receive the same message.
2) Connect RCP TX -> RX (yes to itself), and *could not* receive its own data. So this might be my problem here...

The following two were done using RCP lua script and init(4, 9600, 8, 0, 1) and using the same settings on my ESP8266

3) Connect RCP TX -> ESP RX, and *could not* get any data at all
4) Connect RCP RX -> ESP TX, and *could* get data *but* it was always wrong. e.g. (AT became XZ). And many other things became QPGDM or something (maybe an error message... something about GodDaMn I presume)

Anyhoo, I'm stuck and cannot transmit any UART data between the two.
vroom

Post Reply