Coding begins...

I have the motor controller for the Tesla motor and plan to integrate that with a Raspberry Pi micro computer over a CAN bus for instrumentation (not using it for gear selection -- there are 12v inputs on the controller for gear selection so I plan to fab a unit with detents and a delay relay that will integrate with the original BMW automatic shifter but that's a future project). 

The drive unit controller from 057 technologies communicates via CAN

If you aren't familiar with it, a Controller Area Network (CAN bus) is a two-wire network that allows devices to send and receive messages to/from each other.  Kinda like Ethernet for your car.  All modern cars connect the various computers in the car (ECU, ABS, TCS, etc) via CAN bus as it makes the wiring super simple and prevents manufacturers from having to agree on proprietary interfaces.  My 057 drive unit controller sends and receives messages over CAN, as will the battery management system, on board charger, DC/DC converter, etc.  

By listening to CAN messages from the drive unit controller (DUC), I can know how fast we're going,  throttle position, selected gear, temperatures of various components, etc.  All this is useful information to display in some sort of in-dash display.

By sending CAN messages to the DUC, I can set power limits, turn on/off creep or regenerative braking, set cruise control or traction control, etc.  Therefore, having some sort of input to a dash application could be useful.



Raspberry Pi 4 micro computer



One of the ideas for the digital dash is to put it in place of the tach. Ideally, this would be done with a square display that would fit between the speedo and clock (about 5 inches) but square displays are super hard to come by. Then, a UK company announced a 5 inch square touch display for under $100. Paul and I each bought one, only to realize upon delivery that they are 5 inches diagonally so only about 4" square -- too small for the tach hole. 

The tach hole dash app idea

Back to either a 16:9 in portrait sticking down below the dash or some other solution. I'm actually thinking about putting a horizontal 16:9 behind the speaker grill but haven't fully flushed out that idea yet.   Paul cooked up a design for a rectangular UI that would display some useful information and allow us to set power limits, etc.


However, I couldn't get my Pi to boot with the PICAN2 hat attached. It appeared it might be a faulty board so I sent it back (but it is out of warranty because I bought it more than 30 days ago). While waiting to verify that the board was bad and possible need to purchase a new one, I have yet to successfully write any code to receive CAN messages. Also, the PICAN2 is a kind of lame design where it covers all the GPIO pins on the Pi, even though it only actually uses 4 (or 6, I forget) so I have to fabricate a GPIO bridge to allow simultaneous connection of the CAN hat and access to the i2c pins for the digital pots. 

The PICAN2 board mounted on the Raspberry Pi doesn't pass through the GPIO pins for other use

Oh, and even if the square display had been the right size, it too stacks onto the GPIO pins and covers them all (versus using the HDMI connector) so there is no (easy) way to connect the CAN board and that display at the same time. Live and learn.  Then I got a HDMI-connected 16:9 display. But I struggled to set the proper resolution on it.  Once I got that figured out, I found that the touch screen only works if powered via USB from the pi but the pi doesn't output enough current to drive the display properly.  Sigh.

I originally started programming the Pi in Python, using the PyGame library for graphic control but I found it cumbersome and progress was slow. I've since moved to Electron, targeting Linux for the Pi and it is *so* much better. I put together a sample dash (far from anything we'll ever actually use) just to mess around. There's a node.js module for interfacing with the CAN controller and I was be able to use electron's IPC ability to pass the CAN messages from the node process to the electron client. 

Since messing around with the 5" square display and the troublesome 16:9 display, I've got things figured out.  There is a 16:9 touch screen display specifically made for the pi that uses a small daughter board and works perfectly so that's my original target.  

The pi touchscreen works perfectly

Confirmed PICAN2 board was faulty. Copper Hill Tech stood tall and sent me a replacement for free.  I powered up the replacement PICAN2 CAN bus controller on the Raspberry Pi (after soldering in jumpers for the terminating resistor -- a CAN network needs at least two nodes, with terminating resistors on each end). This new board works perfectly -- the old bad board had me questioning myself. 

I used the switch-mode power supply on the CAN board to power the Pi (no more USB wall wart) and also powered up the Drive Unit Controller as the other CAN node, all using a PC power supply for the 12 volts. 

After some Linux configuration, I was able to successfully run the Linux CAN Utilities and candumped the messages from the Drive Unit Controller. From there, I worked on programming some Inter Process Communication (IPC) from my Electron program's node server to the renderer UI process. That took a while because the newer versions of Electron disable node integration (be sure to specify nodeIntegration: true when initializing the browser window). 

Once that was done, I used the Node.js SocketCAN extensions to receive the messages from the drive unit into the node process and then relay them to the UI. From there, I was able to display the values in my UI. 

This was a major step, with all the pieces finally in place and working. True, most of the message values are zero because I don't have the drive unit controller connected to the drive unit itself yet, but a few values oscillate to prove it is working. From there, I wrote some code to decode the data buffers into values useful to the dash. 

Now I just need to make it all pretty. I've experimented with React and Electron together so next I'll bring react into the app and write some react components for the dash.

Popular posts from this blog

Different strokes

Love that battery

Going Analog in a Digital World