Page 1 of 2

SENT J2716 Protocol

Posted: Fri Apr 15, 2022 5:59 pm
by mtmotorsport
As I sit here waiting for a Proteus and BMW GDI box for testing I came to the realization that the engine I intend to test this on uses SENT protocol for the throttle body position. I haven't seen anything in any menu that I can remember about enabling this protocol for throttle position.

https://en.wikipedia.org/wiki/SENT_(protocol)

Thoughts?

Re: SENT Protocol

Posted: Fri Apr 15, 2022 6:25 pm
by AndreyB
mtmotorsport wrote:
Fri Apr 15, 2022 5:59 pm
Thoughts?
Yep, we've also realized that about a week ago. https://github.com/rusefi/rusefi/issues/4071

There is no plan so far. Is there a way for you to migrate to legacy ETB?

I see an expensive CAN to SENT adapter, do not see a cheap CAN to SEND adapter so far.

Should we do dedicated stm32f103 SENT to CAN adapter?

Re: SENT Protocol

Posted: Fri Apr 15, 2022 7:25 pm
by mtmotorsport
Legacy TB will bolt up directly, no issue there. I suppose I could make a short jumper harness from the throttle body to the ECU, bypassing the current harness.

SENT is normally handled in other ECUs by a fast analog input, or digital input. Some ECUs have dedicated SENT inputs. SENT to CAN could be an option if it is not possible to use the extra crank/cam inputs on Proteus

Re: SENT Protocol

Posted: Fri Apr 15, 2022 7:32 pm
by AndreyB
mtmotorsport wrote:
Fri Apr 15, 2022 7:25 pm
if it is not possible to use the extra crank/cam inputs on Proteus
It's not a hardware issue, it's a software issue. We have 2.5 software developers. I can only speak for myself and I know that I have a zero chance of working on code in the next three months.

Re: SENT Protocol

Posted: Fri Apr 15, 2022 8:05 pm
by mck1117
It's sort of a hardware issue too. SENT is a real pain in the butt to try and decode in software.
mtmotorsport wrote:
Fri Apr 15, 2022 7:25 pm
Legacy TB will bolt up directly, no issue there. I suppose I could make a short jumper harness from the throttle body to the ECU, bypassing the current harness.
Do this.

Re: SENT Protocol

Posted: Tue May 17, 2022 12:12 am
by AndreyB
Several new control modules on 2016 GM models use new sensor technology. The SAE J2716 SENT (Single Edge Nibble Transmission) protocol allows for high resolution data transmission from
a sensor to a control module, such as readings for temperature, pressure, throttle position and mass airflow.
Beginning in 2016, fuel systems on some GM vehicles featured a digital S.E.N.T. (Single Edge Nibble Transmission) fuel rail pressure sensor and SENT technology has become more prevalent in GM models launched after 2016.
ATS (LGX, LF4), XTS (LF3), CTS
(LF3, LGX), Encore (LE2), Camaro (LGX), Malibu (LFV), CT6
(LGX, LGW) and Cruze (LE2,
LFV)
GM 12705503?

Re: SENT Protocol

Posted: Tue May 17, 2022 12:12 am
by AndreyB
https://static.nhtsa.gov/odi/tsbs/2019/MC-10163236-9999.pdf
This purpose of this bulletin is to introduce the completely new 2020 Chevrolet Silverado and GMC
Sierra 2500HD-3500HD pickup trucks. This bulletin will help the Service Department Personnel become
familiar with the all-new 6.6L V8 — RPO L8T gasoline engine.

The fuel rail assembly attaches to each cylinder head. The fuel rail distributes high pressure fuel to the fuel
injectors. The fuel rail assembly consists of the direct fuel injectors and the fuel rail pressure sensor. The fuel
rail pressure sensor transmits fuel pressure and temperature information by serial data using the
Society of Automotive Engineers (SAE) J2716 Single Edge Nibble Transmission (SENT) protocol.
GM 12684286?

https://www.msextra.com/forums/viewtopic.php?t=76289
I pulled one apart to try and determine the pinout. There was a 16pin surface mount chip
image.png
image.png (80.62 KiB) Viewed 33903 times
Also ETB 12678223

Re: SENT Protocol

Posted: Tue May 17, 2022 12:19 am
by AndreyB
The Variable Geometry Turbine (VGT) body assembly contains a contact-less inductive VGT position sensing element that is managed by a
customized integrated circuit. The customized integrated circuit translates the voltage based position information into serial data using Single
Edge Nibble Transmission (SENT) protocol. The VGT position sensor information is transmitted between the VGT body and the ECM on the signal/serial data circuit.
image.png
image.png (158.41 KiB) Viewed 33902 times

Re: SENT Protocol

Posted: Tue May 17, 2022 1:48 am
by AndreyB
Porsche Panamera S looks like they have used SENT before GM?
0261232003?
0261232002 0261232011 0261232012 0261232022 0261232023
06E906051AA 06E906051AB 06E906051L 06E906051N 06E906051Q
image.png
image.png (677.18 KiB) Viewed 33898 times

Re: SENT J2716 Protocol

Posted: Tue May 17, 2022 7:58 pm
by AndreyB
Fun fact: all this reminds Toyota BEAN from back 1990is!

BEAN is based on a protocol developed in 1992 (SAE920231)

Re: SENT J2716 Protocol

Posted: Sat Jun 04, 2022 2:17 am
by AndreyB
12617792
2014-2019 Chevrolet Silverado 1500 Throttle Body

harness side connector TE 1-1419168-1
pins 1393365-2
kit https://www.bmotorsports.com/shop/product_info.php/products_id/4479

#3 BLU/WHT signal we hope SENT even while pic is confusing
#4 BLK/BRN Grount
#5 BRN/RED +5
image.png
image.png (177.18 KiB) Viewed 33573 times

Re: SENT J2716 Protocol

Posted: Fri Jun 10, 2022 12:37 am
by AndreyB
image.png
image.png (109.3 KiB) Viewed 33463 times
which three or more wires would be related to SENT MAF signal?

Re: SENT J2716 Protocol

Posted: Sun Jun 12, 2022 9:40 pm
by AndreyB
12639457AA which seems to point me at 2012 buick regal/saab - and 6 pins

1- motor +
2- motor -
3- TPS Signal (sent)
4- low reference (sensor ground)
5- 5 volt reference
6- not used.

Pin one is closest to the keying notch

https://github.com/rusefi/rusefi/issues/4252

Re: SENT J2716 Protocol

Posted: Sun Jun 19, 2022 8:20 pm
by Simon@FutureProof
Just to add to this a bit, seems some other more mainstream manufacturers are now using sent for the throttle body, I can only assume it is Bosch driving this as that seems to be the common denominator.

Re: SENT J2716 Protocol

Posted: Sun Aug 07, 2022 3:06 am
by AndreyB
I've commissioned a blue pill SENT decoder and it works at least somewhat - in ICU mode it claims 83% of damaged packets on my desk but at least there is 17% of successful CRC validation and position value follows throttle movement.

Fun thing - it's _very_ not linear: linear interpolation between force-open and force-closed position puts typical idle gap at 27%?!

first three nibbles, second three nibbles, linear interpolation of first first three nibble from 435 to 3665

Code: Select all

2803 1292 027 err 004636 000893 008715 000210 010716
2802 1293 027 err 004684 000904 008799 000212 010818
2802 1293 027 err 004732 000913 008890 000215 010927
2797 1298 027 err 004777 000922 008986 000216 011039
2797 1298 027 err 004834 000932 009075 000219 011146
2796 1299 027 err 004875 000940 009151 000220 011242
2797 1298 027 err 004918 000948 009240 000224 011350
2800 1295 027 err 004949 000957 009325 000225 011453
2806 1289 027 err 005005 000966 009429 000226 011572
2810 1285 027 err 005045 000975 009529 000227 011689
2830 1265 026 err 005094 000985 009615 000230 011793
2849 1246 026 err 005145 000994 009696 000235 011892
2920 1175 024 err 005177 001003 009775 000237 011991
3047 1048 020 err 005218 001010 009858 000238 012092
3191 0904 015 err 005256 001022 009946 000240 012198
3410 0685 008 err 005298 001030 010028 000241 012299
3522 0573 005 err 005328 001038 010096 000241 012387
3616 0479 002 err 005367 001044 010175 000245 012485
3652 0443 001 err 005396 001051 010256 000249 012585
3656 0439 001 err 005427 001061 010354 000249 012700
3657 0438 001 err 005466 001068 010454 000252 012817
3657 0438 001 err 005494 001077 010537 000254 012919
3657 0438 001 err 005530 001084 010641 000255 013040
3657 0438 001 err 005561 001092 010719 000256 013137
3657 0438 001 err 005587 001099 010799 000256 013236
3658 0437 001 err 005626 001110 010893 000256 013348
3659 0436 001 err 005650 001118 010968 000258 013443
3660 0435 001 err 005681 001126 011058 000261 013550
3661 0434 001 err 005715 001137 011154 000262 013662

Re: SENT J2716 Protocol

Posted: Sun Aug 07, 2022 5:39 am
by mck1117
AndreyB wrote:
Sun Aug 07, 2022 3:06 am
Fun thing - it's _very_ not linear: linear interpolation between force-open and force-closed position puts typical idle gap at 27%?!
My gen 4 LS throttle (2005-2015 ish, analog) sits at 23%, so this isn't completely unexpected

Re: SENT J2716 Protocol

Posted: Sun Aug 07, 2022 3:40 pm
by AndreyB

Re: SENT Protocol

Posted: Wed Aug 10, 2022 5:33 pm
by AndreyB
AndreyB wrote:
Tue May 17, 2022 12:12 am
GM 12684286
Confirmed to be SENT, $18 shipped from Rockauto

(also looks like 12705503 / 12672582 is NOT SENT - that one is used on other modern GM DI vehicles)

Re: SENT J2716 Protocol

Posted: Wed Aug 10, 2022 5:36 pm
by nmstec
That is noisy as fuck. There 100% must be some filtering/pull on the circuit to make this work properly.

Re: SENT J2716 Protocol

Posted: Sun Aug 14, 2022 10:42 pm
by Dron_Gus
12684286 Setup
20220815_011334.jpg
20220815_011334.jpg (1.67 MiB) Viewed 32247 times
Blue Pill with original STM32F103.
Powered from micro USB.
Connected to JTAG adapter with 4 wires.
Connected to USB-uart with one pin (PA9) - Rx to PC.

GM fuel pressure/temperature sensor from junkyard.
Powered from lab power supply (as per Andrey request). Powering from separate power supply increases error rate to +1 Short Pulse error per 2..3 sec (sometimes, WTF?!).
SENT signal connected to STM32's PA7 through recommended RC filter: 51R + 1nF:
Screenshot from 2022-08-15 01-24-06.png
Screenshot from 2022-08-15 01-24-06.png (26.67 KiB) Viewed 32247 times
No pull-down transistor, as we do not need to speak on SEND bus from STM32 side.

Pull-up: both configuration were tested. With and without 2.2K pull-up to Blue Pull's +5V.
with pull-up
with pull-up
20220815_011615.jpg (870.68 KiB) Viewed 32247 times
with NO pull-up
with NO pull-up
20220815_011807.jpg (920.74 KiB) Viewed 32247 times
With pull-up you can see that sensor is able to pull low only to 1V. Without pull-up it reaches 0.3V.
"+1 Short Pulse error per 2..3 seconds" I mentioned before is observed only with pull-up enabled.

Code: Select all

nibbles: ST=0 D=0:C:5:B:C:0 CRC=8 Errs Short: 000001 Long: 000000 Sync: 000000 CRC: 000000 (000 %). Frames: 008202
 GM: St 4, Sig0 0198, Sig1 3008. Tick = 2597 nS.
  msg 22: 0x0302 (770), T = 24.06250C ?
  msg 16: 0x0303 (771), T = 24.09375C ?
nibbles: ST=C D=0:C:5:B:C:0 CRC=4 Errs Short: 000001 Long: 000000 Sync: 000000 CRC: 000000 (000 %). Frames: 008392
 GM: St C, Sig0 0197, Sig1 3008. Tick = 2597 nS.
  msg 22: 0x0302 (770), T = 24.06250C ?
  msg 16: 0x0303 (771), T = 24.09375C ?
nibbles: ST=0 D=0:C:5:A:C:0 CRC=3 Errs Short: 000001 Long: 000000 Sync: 000000 CRC: 000000 (000 %). Frames: 008583
 GM: St 0, Sig0 0197, Sig1 2752. Tick = 2597 nS.
  msg 22: 0x0302 (770), T = 24.06250C ?
  msg 16: 0x0303 (771), T = 24.09375C ?
nibbles: ST=8 D=0:C:5:B:C:0 CRC=3 Errs Short: 000001 Long: 000000 Sync: 000000 CRC: 000000 (000 %). Frames: 008773
 GM: St C, Sig0 0197, Sig1 2752. Tick = 2597 nS.
  msg 22: 0x0301 (769), T = 24.03125C ?
  msg 16: 0x0303 (771), T = 24.09375C ?
nibbles: ST=4 D=0:C:5:B:C:0 CRC=4 Errs Short: 000001 Long: 000000 Sync: 000000 CRC: 000000 (000 %). Frames: 008965
 GM: St 4, Sig0 0197, Sig1 3008. Tick = 2597 nS.
  msg 22: 0x0302 (770), T = 24.06250C ?
  msg 16: 0x0303 (771), T = 24.09375C ?
nibbles: ST=4 D=0:C:5:B:C:0 CRC=4 Errs Short: 000001 Long: 000000 Sync: 000000 CRC: 000000 (000 %). Frames: 009155
 GM: St 4, Sig0 0197, Sig1 3008. Tick = 2597 nS.
  msg 22: 0x0302 (770), T = 24.06250C ?
  msg 16: 0x0303 (771), T = 24.09375C ?
nibbles: ST=C D=0:C:5:B:C:0 CRC=4 Errs Short: 000001 Long: 000000 Sync: 000000 CRC: 000000 (000 %). Frames: 009346
 GM: St C, Sig0 0197, Sig1 3008. Tick = 2597 nS.
  msg 22: 0x0302 (770), T = 24.06250C ?
  msg 16: 0x0303 (771), T = 24.09375C ?
nibbles: ST=0 D=0:C:5:A:C:0 CRC=3 Errs Short: 000001 Long: 000000 Sync: 000000 CRC: 000000 (000 %). Frames: 009537
 GM: St 0, Sig0 0197, Sig1 2752. Tick = 2597 nS.
  msg 22: 0x0302 (770), T = 24.06250C ?
  msg 16: 0x0303 (771), T = 24.09375C ?
Some test results:
1. When sensor supply voltage is lowered to about 3.5 it starts sending 0xF in main SENT frames, but still sends correct (?) temperature in slow channels:

Code: Select all

nibbles: ST=8 D=F:F:F:F:F:F CRC=A Errs Short: 000002 Long: 000000 Sync: 000000 CRC: 000000 (000 %). Frames: 009280
 GM: St C, Sig0 4095, Sig1 4095. Tick = 2597 nS.
  msg 16: 0x02F9 (761), T = 23.78125C ?
  msg 22: 0x02FC (764), T = 23.87500C ?
2. Disabling input RC filter (by shorting R) increases Short Pulse error rate to ~10 per second.
3. Adding 2.2K pull-up to 5V also increases Short Pulse error rate +0.3 err/sec

Re: SENT J2716 Protocol

Posted: Sun Aug 14, 2022 10:47 pm
by Dron_Gus
Questions:
1. still not clear not to convert raw data from SEND message:

Code: Select all

 D=0:C:5:B:C:0
to pressure
2. Temperature is converted with simple divide by 32. What about temperature below zero?
3. What is purpose of 4.7nF capacitor directly connected to bus? Looks like this is emulation of long bus "The overall resistance of all connectors is limited to 1 Ω, the bus wiring to 0.1nF/m capacitance and the maximum cable length is limited to 5 m." Should we add this cap for table tests?
Screenshot from 2022-08-15 01-24-06.png
Screenshot from 2022-08-15 01-24-06.png (26.67 KiB) Viewed 32243 times

Re: SENT J2716 Protocol

Posted: Mon Aug 15, 2022 10:10 am
by Dron_Gus
Night of working:

Code: Select all

nibbles: ST=4 D=0:C:6:B:C:0 CRC=8 Errs Short: 044703 Long: 002951 Sync: 002652 CRC: 002640 (000 %). Frames: 75146449
 GM: St 4, Sig0 0198, Sig1 3008. Tick = 2597 nS.
  msg 22: 0x0316 (790), T = 24.68750C ?
  msg 16: 0x0318 (792), T = 24.75000C ?

Re: SENT J2716 Protocol

Posted: Mon Aug 15, 2022 11:32 pm
by AndreyB
50R+1nf RC filter combined with https://github.com/rusefi/rusefi-hardware/pull/148 total success for the fuel pressure sensor!


Re: SENT J2716 Protocol

Posted: Tue Aug 16, 2022 1:51 am
by AndreyB
Nick says VAG 06E906054 06E906054J oil sensor?

Also not just Porsche but other VAG use SENT for MAP sensor part number unknown and we do not really care at this point.

Re: SENT J2716 Protocol

Posted: Tue Aug 16, 2022 1:59 am
by AndreyB
AndreyB wrote:
Sun Jun 12, 2022 9:40 pm
12639457AA which seems to point me at 2012 buick regal/saab - and 6 pins

Re: SENT J2716 Protocol

Posted: Tue Aug 23, 2022 10:04 pm
by Dron_Gus
AndreyB wrote:
Sun Jun 12, 2022 9:40 pm
12639457AA which seems to point me at 2012 buick regal/saab - and 6 pins
Also Opel Insignia 2.0 turbo A20NFT engine.

Code: Select all

nibbles: ST=0 D=5:C:F:0:3:A CRC=3 Errs Short: 000043 Long: 000000 Sync: 000003 CRC: 021783 (090 %). Frames: 024048
 Throt: 36% (1481) : 63% (2614), sum = 99 (4095)
nibbles: ST=0 D=5:E:5:A:1:A CRC=9 Errs Short: 000043 Long: 000000 Sync: 000003 CRC: 021911 (090 %). Frames: 024198
 Throt: 36% (1505) : 63% (2590), sum = 99 (4095)
nibbles: ST=0 D=6:0:3:C:F:9 CRC=C Errs Short: 000043 Long: 000000 Sync: 000003 CRC: 022047 (090 %). Frames: 024347
 Throt: 37% (1534) : 62% (2561), sum = 99 (4095)
nibbles: ST=0 D=6:7:8:7:8:9 CRC=9 Errs Short: 000043 Long: 000000 Sync: 000003 CRC: 022175 (090 %). Frames: 024496
 Throt: 40% (1640) : 59% (2455), sum = 99 (4095)
nibbles: ST=0 D=9:3:8:7:C:6 CRC=C Errs Short: 000043 Long: 000000 Sync: 000003 CRC: 022307 (090 %). Frames: 024645
 Throt: 56% (2334) : 42% (1761), sum = 99 (4095)
nibbles: ST=0 D=A:9:0:F:6:5 CRC=6 Errs Short: 000043 Long: 000000 Sync: 000003 CRC: 022436 (090 %). Frames: 024794
 Throt: 66% (2711) : 33% (1384), sum = 99 (4095)
nibbles: ST=0 D=A:7:A:4:8:5 CRC=C Errs Short: 000043 Long: 000000 Sync: 000003 CRC: 022580 (090 %). Frames: 024944
 Throt: 65% (2686) : 34% (1409), sum = 99 (4095)
nibbles: ST=0 D=8:0:6:9:F:7 CRC=9 Errs Short: 000044 Long: 000000 Sync: 000003 CRC: 022710 (090 %). Frames: 025093
 Throt: 51% (2090) : 48% (2005), sum = 99 (4095)
nibbles: ST=0 D=5:D:9:6:2:A CRC=9 Errs Short: 000044 Long: 000000 Sync: 000003 CRC: 022842 (090 %). Frames: 025242
 Throt: 36% (1501) : 63% (2594), sum = 99 (4095)
nibbles: ST=0 D=5:D:F:0:2:A CRC=A Errs Short: 000044 Long: 000000 Sync: 000003 CRC: 022979 (090 %). Frames: 025394
 Throt: 36% (1501) : 63% (2594), sum = 99 (4095)
nibbles: ST=0 D=6:0:8:7:F:9 CRC=C Errs Short: 000044 Long: 000000 Sync: 000003 CRC: 023106 (090 %). Frames: 025543
 Throt: 37% (1542) : 62% (2553), sum = 99 (4095)
nibbles: ST=0 D=7:5:4:B:B:8 CRC=3 Errs Short: 000044 Long: 000000 Sync: 000003 CRC: 023235 (090 %). Frames: 025692
 Throt: 43% (1776) : 56% (2319), sum = 99 (4095)
nibbles: ST=0 D=A:8:1:E:7:5 CRC=A Errs Short: 000044 Long: 000000 Sync: 000003 CRC: 023365 (090 %). Frames: 025842
 Throt: 65% (2686) : 34% (1409), sum = 99 (4095)
nibbles: ST=0 D=A:9:9:6:6:5 CRC=C Errs Short: 000044 Long: 000000 Sync: 000003 CRC: 023493 (090 %). Frames: 025992
 Throt: 66% (2716) : 33% (1379), sum = 99 (4095)
nibbles: ST=0 D=9:9:6:9:6:6 CRC=9 Errs Short: 000044 Long: 000000 Sync: 000003 CRC: 023620 (090 %). Frames: 026141
 Throt: 61% (2531) : 38% (1564), sum = 99 (4095)
nibbles: ST=0 D=7:0:B:4:F:8 CRC=0 Errs Short: 000044 Long: 000000 Sync: 000003 CRC: 023747 (090 %). Frames: 026290
 Throt: 44% (1806) : 55% (2289), sum = 99 (4095)
nibbles: ST=0 D=6:F:C:3:0:9 CRC=6 Errs Short: 000044 Long: 000000 Sync: 000003 CRC: 023884 (090 %). Frames: 026439
 Throt: 43% (1787) : 56% (2308), sum = 99 (4095)
nibbles: ST=0 D=7:4:7:9:B:8 CRC=C Errs Short: 000045 Long: 000000 Sync: 000003 CRC: 024020 (090 %). Frames: 026587
 Throt: 45% (1859) : 54% (2236), sum = 99 (4095)
nibbles: ST=0 D=8:4:3:C:B:7 CRC=9 Errs Short: 000045 Long: 000000 Sync: 000003 CRC: 024153 (090 %). Frames: 026736
 Throt: 51% (2101) : 48% (1994), sum = 99 (4095)
nibbles: ST=0 D=9:F:4:B:0:6 CRC=F Errs Short: 000045 Long: 000000 Sync: 000003 CRC: 024284 (090 %). Frames: 026886
 Throt: 60% (2490) : 39% (1605), sum = 99 (4095)
nibbles: ST=0 D=9:F:3:C:0:6 CRC=9 Errs Short: 000045 Long: 000000 Sync: 000003 CRC: 024380 (090 %). Frames: 027036
 Throt: 62% (2551) : 37% (1544), sum = 99 (4095)
nibbles: ST=0 D=9:9:8:7:6:6 CRC=5 Errs Short: 000045 Long: 000000 Sync: 000003 CRC: 024514 (090 %). Frames: 027186
 Throt: 60% (2469) : 39% (1626), sum = 99 (4095)

Re: SENT J2716 Protocol

Posted: Thu Oct 20, 2022 9:05 pm
by AndreyB
@Dron_Gus is the mother of rusEFI SENT, current microRusEFI snapshot firmware has one SENT input channel. More info once it works more reliably!

Re: SENT J2716 Protocol

Posted: Wed Nov 02, 2022 6:29 pm
by AndreyB
Ford uses 0280750688
Note the cute "digital output" comment

Throttle Body For Ford Mustang 2018 2019 2020 5.0L and the Godzilla engine!
image.png
image.png (659.29 KiB) Viewed 30722 times

Re: SENT J2716 Protocol

Posted: Wed Nov 09, 2022 1:43 am
by AndreyB
AndreyB wrote:
Wed Nov 02, 2022 6:29 pm
Ford uses 0280750688
Cute simplicity
image.png
image.png (17.37 KiB) Viewed 30500 times

Re: SENT J2716 Protocol

Posted: Sun Nov 13, 2022 11:27 pm
by AndreyB
Time to declare mission accomplished



https://github.com/rusefi/rusefi/wiki/SENT-ETB-Electronic-Throttle-Body
tpsSensor = Sensor.new("Tps1")
tpsSensor : setRedundant(true)

ppsSensor = Sensor.new("AcceleratorPedal")
ppsSensor : setRedundant(true)

Tps1Primary = Sensor.new("Tps1Primary")
AcceleratorPedalPrimary = Sensor.new("AcceleratorPedalPrimary")

setTickRate(100)
-- huh? with 100Hz tick rate why do we need such huge timeout?
ppsSensor : setTimeout(1000)
tpsSensor : setTimeout(1000)
restartEtb()

function setTwoBytes(data, offset, value)
value = math.floor(value)
data[offset + 2] = value >> 8
data[offset + 1] = value & 0xff
end


-- CAN Rx Channels
IDRIVE_BUTTONS = 0x267
IDRIVE_ROTARY = 0x264
-- CAN Tx Channels
IDRIVE_BRIGHTNESS = 0x202
IDRIVE_HEARBEAT = 0x560
IDRIVE_ROTARY_INIT = 0x273
canRxAdd(IDRIVE_ROTARY) -- rotation
canRxAdd(IDRIVE_BUTTONS) -- buttons

brighness = 0

R52_RPM = 0x316

data = { 0, 0, 0, 00, 0, 0 , 0, 0 }

rpm = 7000

i = 0

-- One time rotary initialisation

txCan(1, IDRIVE_ROTARY_INIT, 0, { 0x1D, 0xE1, 0x00, 0xF0, 0xFF, 0x7F, 0xDE, 0x04 })


clicks = 5

function onCanRx(bus, id, dlc, data)
-- print('got CAN id=' ..id ..' dlc=' ..dlc)

if id == IDRIVE_BUTTONS then
-- print('got buttons @4=' ..data[4] ..' @6=' ..data[6])

if data[4] == 1 or data[4] == 2 then
print('got hold or release @4=' ..data[4] ..' @6=' ..data[6])

if data[6] == 2 and brighness > 0 then
brighness = brighness - 1
print('brighness ' ..brighness)
end

if data[6] == 4 and brighness < 15 then
brighness = brighness + 1
print('brighness ' ..brighness)
end
end
end

if id == IDRIVE_ROTARY then
if data[5] == 0x80 then
clicks = data[4]
print('rotary turned right ' ..data[4] ..' clicks')
end

if data[5] == 0x7f then
print('rotary turned left ' ..255 - data[4] ..' clicks')
end
end

end

function onTick()
-- wake up, need to send once every 1.5 second but we can send more often because why not
txCan(1, IDRIVE_HEARBEAT, 0, { 3 })
txCan(1, IDRIVE_BRIGHTNESS, 0, { brighness * 16 + 0xd })




rpm = getSensor("RPM")

if i == 3 then
print('Postponed restart')
restartEtb()
-- i = 0
end
-- print('Period ' ..i)

i = i + 1


setTwoBytes(data, 2, rpm * 6.39)

txCan(1, R52_RPM, 0, data)


-- 1 ABS and warm-up light but no tach
-- 3 blinker and alive
-- 4 ABS and alive
--
status2 = 2

-- status3 = 0xC0
status3 = 0x0

status4 = 0x0
-- status4 = 0xC0
-- with ABS with blinker with warm-up light
-- status4 = 0xFF

txCan(1, 0x61f, 0, { 0, 0, status2, status3, status, 0 , 0, 0 })




sentValue = getSentValue(1)
-- tpsValue = interpolate(0.11, 0, 0.89, 100, sentValue)
tpsValue = interpolate(0.11, 100, 0.89, 0, sentValue)

if tpsValue == tpsValue then
tpsSensor : set(tpsValue )
Tps1Primary : set(tpsValue )
-- print('sentValue ' ..sentValue..' tps ' ..tpsValue)
else
-- print('sentValue is Nan')
end



ppsSensor : set(5 * clicks)

end