Pi data to Arduino via SPI

The more technical aspects of Spirit, and carry-over discussion from Kickstarter updates.
Kevin
Posts: 179
Joined: Tue Jul 28, 2015 12:56 am

Pi data to Arduino via SPI

Postby Kevin » Sat Feb 10, 2018 2:57 am

Hey all. This has been mentioned a few times so I wanted to post a new thread for it.

The Pi does indeed connect directly to the Arduino via a SPI bus, and as far as I can tell, it works as expected.

We already have a SPI protocol implemented between the two devices that you can latch on to with your own code.

Here's the quick overview...

The Arduino has a SPI buffer length of 11 bytes (this is set in the Comms.h tab of Arduino). This allows for 1 "register address", 8 bytes usable for your data, 1 byte for a checksum (if you want to add it), and 1 trailing byte, which needs to be sent if you're reading the full response string as SPI clocks the outgoing bytes one behind the incoming bytes.

Look at the SPI_Handler function in the Comms tab, and see how we use it to control motors. The first byte sent in the string is intended to "key" the string to a certain behavior or handler on the other end.

We reserve the value 130 to control motors, and you shouldn't use 255 as it's a catch all needed to ignore occasional data errors from teh Pi.

On the Arduino look at SPI_BufferIn and SPI_BufferOut in the Comms tab, and look at all the SPI functions near the bottom of that tab. You should be able to add your own hooks in there for other stuff.

It's hard to process data and pack up the outgoing buffer fast enough to do on the fly, so you may want to send and query back any data in separate packets, so you have time to load up the outgoing buffer on the Arduino side before it's needed by the Pi.

To pack up a packet from the Pi to the Arduino, have a look at the motors function in the spirit_core.py.

Be careful to not flood the SPI bus with too much traffic - I think it'll work fine, but remember the Arduino needs a short time to deal with the incoming data. Maybe add some real short pauses (like maybe 100 to 500 microseconds?) between sequential SPI sends from the Pi. It may work faster, but if you're having dropped data, this may be the problem.

That's not super detailed but hopefully useful to someone.

-Kevin

gpvillamil
Posts: 75
Joined: Fri Jan 26, 2018 5:03 pm

Re: Pi data to Arduino via SPI

Postby gpvillamil » Sat Feb 10, 2018 4:24 am

How feasible is it to do what is described in this article:

https://learn.adafruit.com/program-an-a ... s/overview

User avatar
esba1ley
Posts: 58
Joined: Sun Jan 01, 2017 2:15 am
Location: Pasadena, CA
Contact:

Re: Pi data to Arduino via SPI

Postby esba1ley » Sun Feb 11, 2018 1:32 am

Again, seconded. Being able to program the Arduino from the on-board PI via SPI would be awesome. That way we could treat the Pi as a development workstation when needed -- no external computer necessary, if you plug in an HDMI cable and have a bluetooth keyboard/mouse configured to use with the Pi.

This also opens up the possibility of re-configuring the Arduino code based on a higher level decision making being done on the Pi. Say you're robot has a bunch of autonomous modes that are off-loaded to the Arduino, but you want to change the behavior based on something that the RasPi processes via the camera... you could in theory have the .hex files all pre-compiled waiting, and script avrdude to flash the arduino with the code from the main Pi as needed to "change the mode" on the arduino. This could allow for more complex mode switching, pushing the automation down into the Arduino to free the Pi to do more heavy lifting (like vision processing).

Just a thought.

-E
----------
Erik S. Bailey
Entry, Descent, and Landing Aerospace Engineer
Maker, and Musician
SpiritRover Kickstarter Backer #460

gpvillamil
Posts: 75
Joined: Fri Jan 26, 2018 5:03 pm

Re: Pi data to Arduino via SPI

Postby gpvillamil » Sun Feb 11, 2018 10:21 pm

This is such an interesting idea! The 32k of flash on the Arduino is not a lot, but you could keep a lot of .hex files on the Pi. I wonder if you can shutdown and restart the Pi from the Arduino? You could shut down the Pi to save power, and only restart it when needed. Run only essential processes on a low power processor, with the ability to change those processes dynamically...

esba1ley wrote:This also opens up the possibility of re-configuring the Arduino code based on a higher level decision making being done on the Pi. Say you're robot has a bunch of autonomous modes that are off-loaded to the Arduino, but you want to change the behavior based on something that the RasPi processes via the camera... you could in theory have the .hex files all pre-compiled waiting, and script avrdude to flash the arduino with the code from the main Pi as needed to "change the mode" on the arduino. This could allow for more complex mode switching, pushing the automation down into the Arduino to free the Pi to do more heavy lifting (like vision processing).

Just a thought.

-E

User avatar
esba1ley
Posts: 58
Joined: Sun Jan 01, 2017 2:15 am
Location: Pasadena, CA
Contact:

Re: Pi data to Arduino via SPI

Postby esba1ley » Sun Feb 11, 2018 10:51 pm

FWIW, FPGAs do something similar -- they can literally reconfigure their *hardware* while running, if designed properly. Pretty cool stuff. This would open students' worlds into this concept.

-E
----------
Erik S. Bailey
Entry, Descent, and Landing Aerospace Engineer
Maker, and Musician
SpiritRover Kickstarter Backer #460


Who is online

Users browsing this forum: No registered users and 1 guest