<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.autosportlabs.com/index.php?action=history&amp;feed=atom&amp;title=Cosworth_CAN</id>
	<title>Cosworth CAN - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.autosportlabs.com/index.php?action=history&amp;feed=atom&amp;title=Cosworth_CAN"/>
	<link rel="alternate" type="text/html" href="https://wiki.autosportlabs.com/index.php?title=Cosworth_CAN&amp;action=history"/>
	<updated>2026-10-12T01:48:11Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>https://wiki.autosportlabs.com/index.php?title=Cosworth_CAN&amp;diff=6136&amp;oldid=prev</id>
		<title>Brentp: initial channel for testing</title>
		<link rel="alternate" type="text/html" href="https://wiki.autosportlabs.com/index.php?title=Cosworth_CAN&amp;diff=6136&amp;oldid=prev"/>
		<updated>2015-02-27T05:23:23Z</updated>

		<summary type="html">&lt;p&gt;initial channel for testing&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
--Cosworth CAN bus integration&lt;br /&gt;
&lt;br /&gt;
--how frequently we poll for CAN messages&lt;br /&gt;
tickRate = 30&lt;br /&gt;
--the CAN baud rate&lt;br /&gt;
CAN_baud = 1000000&lt;br /&gt;
--CAN channel to listen on. 0=first CAN channel, 1=second&lt;br /&gt;
CAN_chan = 0&lt;br /&gt;
--1 for Big Endian (MSB) mode; 0 for Little Endian mode (LSB)&lt;br /&gt;
be_mode = 1&lt;br /&gt;
&lt;br /&gt;
--add your virtual channels here&lt;br /&gt;
tpsId = addChannel(&amp;quot;TPS&amp;quot;, 10, 0, 0, 100, &amp;quot;%&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
--customize here for CAN channel mapping&lt;br /&gt;
--format is: [CAN Id] = function(data) map_chan(&amp;lt;channel id&amp;gt;, data, &amp;lt;CAN offset&amp;gt;, &amp;lt;CAN length&amp;gt;, &amp;lt;multiplier&amp;gt;, &amp;lt;adder&amp;gt;)&lt;br /&gt;
CAN_map = {&lt;br /&gt;
[256] = function(data) map_chan(tpsId, data, 7, 1, 1, 0) end&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function onTick()&lt;br /&gt;
    processCAN(CAN_chan)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--===========do not edit below===========&lt;br /&gt;
function processCAN(chan)&lt;br /&gt;
    repeat&lt;br /&gt;
        local id, e, data = rxCAN(chan)&lt;br /&gt;
        if id ~= nil then&lt;br /&gt;
            local map = CAN_map[id]&lt;br /&gt;
            if map ~= nil then&lt;br /&gt;
                map(data)         &lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    until id == nil&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--Map CAN channel, little endian format&lt;br /&gt;
function map_chan_le(cid, data, offset, len, mult, add)&lt;br /&gt;
    offset = offset + 1&lt;br /&gt;
    local value = 0&lt;br /&gt;
    local shift = 1&lt;br /&gt;
    while len &amp;gt; 0 do&lt;br /&gt;
        value = value + (data[offset] * shift)&lt;br /&gt;
        shift = shift * 256&lt;br /&gt;
        offset = offset + 1&lt;br /&gt;
        len = len - 1&lt;br /&gt;
    end&lt;br /&gt;
    setChannel(cid, (value * mult) + add)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--Map CAN channel, big endian format&lt;br /&gt;
function map_chan_be(cid, data, offset, len, mult, add)&lt;br /&gt;
    offset = offset + 1&lt;br /&gt;
    local value = 0&lt;br /&gt;
    while len &amp;gt; 0 do&lt;br /&gt;
        value = (value * 256) + data[offset]&lt;br /&gt;
        offset = offset + 1&lt;br /&gt;
        len = len - 1&lt;br /&gt;
    end&lt;br /&gt;
    setChannel(cid, (value * mult) + add)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
map_chan = (be_mode == 1) and map_chan_be or map_chan_le&lt;br /&gt;
initCAN(CAN_chan, CAN_baud)&lt;br /&gt;
setTickRate(tickRate)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Brentp</name></author>
	</entry>
</feed>