Can't query virtual channels mapped via CAN

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

Post Reply
madjak
Posts: 29
Joined: Mon Jul 13, 2015 7:40 am

Can't query virtual channels mapped via CAN

Post by madjak »

I have the RCP working atm but I'm pretty sure how I've implemented it isn't ideal.

Basically I'm using the standard CAN script to read the CAN data and map it to virtual channels, however once mapped, Lua can query the value for use in other scripts. For example, I'm mapping rpmId via the CAN, I can see it on the dashboard but can't query the variable rpmId in Lua even via a print(rpmId).

here is a snippet of my code...

Code: Select all

Can_map = &#123;
&#91;864&#93; = function&#40;data&#41; 
    map_chan&#40;rpmId, data, 0, 2, 1, 0&#41;
end
&#125;

onTick&#40;&#41;
  processCAN&#40;&#41;
  if rpmId > 5000 then setAnalog&#40;1,10&#41; end
  print &#40;rpmId&#41;
end

function processCAN&#40;&#41;
  repeat
    local id, e, data = rxCAN&#40;0&#41;
    if id ~= nil then
      local map = CAN_map&#91;id&#93;
      if map ~= nil then
        map&#40;data&#41; --this runs map_chan function with the CAN 'data'
      end
    end
  until id == nil
end

function map_chan&#40;cid, data, offset, len, mult, add&#41; --eg &#40;rpmId, data, 0, 2, 1, 0&#41;
  offset = offset + 1
  local value = 0
  while len > 0 do
    value = &#40;value * 256&#41; + data&#91;offset&#93;
    offset = offset + 1
    len = len - 1
  end
  setChannel&#40;cid, &#40;value * mult&#41; + add&#41;
end
processCAN() is your standard CAN function from the wiki.

So the lights won't work, and no print will occur. It's like the variable rpmId doesn't exist. Once the virtual channel is mapped the variable rpmId returns nothing. Am I meant to query the mapped channel differently?

What I've done to get around this issue is build an array of values for each of my variables and query that instead. Basically it places rpmId in position 1, mapId in position 2, tpsId in pos3 etc. Here is a snippit of my code:

Code: Select all

VarArray = &#123;0,0,0,0,0,0,0,0,0&#125;

onTick&#40;&#41;
  ProcessCAN&#40;&#41;
  if VarArray&#91;1&#93; > 5000 then setAnalog&#40;1,10&#41; end
  print &#40;VarArray&#91;1&#93;&#41;
end

Can_map = &#123;
&#91;864&#93; = function&#40;data&#41; 
    map_chan&#40;1, rpmId, data, 0, 2, 1, 0&#41;
    map_chan&#40;2, mapId, data, 2, 2, 0.1, -101.3&#41;
    map_chan&#40;3, tpsId, data, 4, 2, 0.1, 0&#41;
end
&#125;

function map_chan&#40;var, cid, data, offset, len, mult, add&#41;
  offset = offset + 1
  local value = 0
  while len > 0 do
    value = &#40;value * 256&#41; + data&#91;offset&#93;
    offset = offset + 1
    len = len - 1
  end
  setChannel&#40;cid, &#40;value * mult&#41; + add&#41;
  VarArray&#91;var&#93; = &#40;value * mult&#41; + add
end
This isn't the nicest way to query the variables but it works. Once I map the virtual channel in the CAN script how do I then query it and use it in Lua?

rdoherty
Posts: 215
Joined: Fri Mar 08, 2013 3:32 am

Post by rdoherty »

Hi madjak, the reason why rpmId doesn't exist is because it's never set. You need to first create the RPM virtual channel like so:

rpmId = addChannel("RPM", 10, 0, 0, 10000)

Then you can pass the rpmId to the CAN map. To be clear 'rpmId' is *not* the current RPM value, it is simply a number for the Luascript to keep track of virtual channels.

Hope that helps!
Ryan Doherty
Autosports Labs

Post Reply