Message Boards Message Boards

2
|
18512 Views
|
12 Replies
|
3 Total Likes
View groups...
Share
Share this post:
GROUPS:

Pulse Width Modulation using Mathematica on the RPi

To my knowledge the "GPIO" device driver that ships with the RPi version of Mathematica doesn't handle pulse width modulation. I was interested in playing around with LEDs, making them flicker like candles. Essentially, I wanted to reproduce this python code in Mathematica.

I approached this problem by creating a shared library that can be accessed using Mathematica's LibraryFunctionLoad. The shared library uses Gordon's wiringPi functions to interface with the GPIO on the RPi. (For what it's worth, this method, while seriously lacking in error checking, results in GPIO access frequencies of up to 13 kHz, which is much higher than what DeviceWrite["GPIO"...] can do at this point.)

In the python code referenced above, the flickering effect is obtained by randomizing the brightness and the update interval of the LED. A high update interval is used to create a high "wind" effect. I was able to take a different approach in Mathematica by using a BinomialDistribution. This results in what I think is a more realistic flickering effect and the influence of wind can be simulated by a lower p-value (corresponding to a broader distribution of brightnesses.) If you are interested, please have a look at my video demonstrating the results. I encourage you to try it out for yourself, though, since the flickering effect doesn't really translate well under the conditions I filmed this video.

There are a couple cool, if silly, things that can be done now that we have PWM in Mathematica. I show how you can create a fading LED with the Sin[] function, and we can even go so far as to visualize solutions to differential equations (like the documentations demonstration of a bouncing ball) using the LED. I show that at the end of the video.

I keep the code at my github repository and would be interested in hearing comments if you happen to give it a try.

POSTED BY: BoB LeSuer
12 Replies

Thanks for the update, Hans. I'm glad you've been able to make something out of WiringPiLink. (And I'm particularly impressed that you got the remote kernel up and running; I have found it very buggy and temperamental, but as you've no doubt noticed, being able to run the interface on a normal computer makes the Wolfram/RPi experience much more enjoyable.)

For what it's worth, Raspbian Jessie appears to drop the need for GPIO users to be sudo'd. I am finishing up another project at the moment, but my intention is to revisit WiringPiLink to make sure it works with the current hardware/software. Stay tuned.

POSTED BY: BoB LeSuer
Posted 9 years ago

Thanks again for your efforts here BoB. Managed to get my class of first time users of Raspberry Pi and who had never coded anything before to build an RC type vehicle out of lego (precision blocks with great range of gearing and steering systems) and control it over wifi using two DC motors, an L293E and an RPi2; powered with a portable $9 5V 1A power supply. All coding was done with Wolfram Language, and with wiringPiLink, PWM was a walk in the park.

I was wondering whether there was any way to call wiringPiLink` from a mathematica session running a remote kernel on the pi from my macbook?

It turns out you can. Which is super great!!

I installed wiringPiLink, following previous instruction, but changing the directory to /usr/bin/. After opening a remote kernel session in Mathematica 10.2.0.0 on my macbook with the following remote kernel advanced options configuration:
Remote Kernel Configuration: Advanced Options

And after not too much fussing (mostly just a bit of reading up on Get[ ] it turns out that running

Get["wiringPiLink`",Path->{"/usr/bin/wiringPiLink"}]

successfully loads the library and we now have the slightly more polished experience of MMA10 with wiringPiLink.

wiringPiLink via remote kernel

POSTED BY: Hans Schmidt
Posted 9 years ago

Thanks BoB! As soon as I enabled SPI it all worked a perfectly. Looking forward to using it. Cheers Hans

POSTED BY: Hans Schmidt
Posted 9 years ago

Hi BoB,

I have just the lib file in the Linux-ARM directory

~/test/wiringPiLink/wiringPiLink/LibraryResources/Linux-ARM $ ls
libwiringpi.so.1.0.2

Running sudo wolfram from ~/test/wiringPiLink

pi@raspberrypi ~/test/wiringPiLink $ sudo wolfram

still produces an SPI error

  In[1]:= <<wiringPiLink`                                                         
    Unable to open SPI device: No such file or directory

I then ran gpio load spi and got an error

pi@raspberrypi ~/test/wiringPiLink $ gpio load spi
ERROR: could not insert 'spi_bcm2708': No such device
gpio: Unable to load spi_bcm2708

pi@raspberrypi ~ $ gpio load spi
ERROR: could not insert 'spi_bcm2708': No such device
gpio: Unable to load spi_bcm2708

hmm

POSTED BY: Hans Schmidt

I think we are close. That error indicates that you don't have the SPI interface enabled, which isn't necessary for blinky lights, but I started to work on some MCP3008 interfacing, which does use SPI, so it needs to be enabled in order to get the library to work.

sudo raspi-config

under advanced options, select SPI and enable it. You can also select to have it automatically loaded at boot (which should prevent the need to do gpio load spi). Reboot and give it a shot.

POSTED BY: BoB LeSuer
Posted 9 years ago

Hi Bob,

Thanks for the reply, I was able to follow your install instructions seemingly without any problems, however I can’t load the library from sudo wolfram.

All smooth sailing with github clone:

pi@raspberrypi ~ $ git clone git://github.com/bobthechemist/wiringPiLink.git
Cloning into 'wiringPiLink'...
remote: Counting objects: 31, done.
remote: Total 31 (delta 0), reused 0 (delta 0), pack-reused 31
Receiving objects: 100% (31/31), 7.93 KiB, done.
Resolving deltas: 100% (10/10), done.

compile.sh neatly produces two files libwiringpi.so.1.0.2 and wiringpilink.o

With the file copied into the Linux-ARM directory (using cp ~/test/wiringPiLink/libwiringpi.so.1.0.2 wiringPiLink/LibraryResources/Linux-ARM) and running inside sudo wolfram, I get the following:

In[1]:= <<wiringPiLink`                                                         
Unable to open SPI device: No such file or directory

I have Gordon’s most recent wiringPi installation in ~/wiringPi and am able to compile and run his examples. It'd be great to know if you have any ideas where I have made an error, staying inside Mathematica to access pwm is awesome and I’d love to get your code up and running.

Cheers, Hans

POSTED BY: Hans Schmidt

Hans,

Please try one more thing; make sure the the lib file is in ~/test/wiringPiLink/wiringPiLink/LinuxResources/Linux-ARM and that you are running sudo wolfram from ~/test/wiringPiLink (the directory tree structure is quirky, I know). I'm not convinced this is the actual problem, since I get a different error when I reproduce this. However, I want to rule out directory structure.

POSTED BY: BoB LeSuer

Hans,

Also (and perhaps try this first) from the command prompt type gpio load spi prior to starting wolfram.

POSTED BY: BoB LeSuer
Posted 9 years ago

Great post Bob, thanks for sharing. It'd be nice to have DeviceWrite work fast enough for PWM straight out of the box though. I haven't been able to get it to work following using your github files on my pi yet, but will certainly keep persisting. Have you got yourself an RPi2 and tested it out on that yet?

POSTED BY: Hans Schmidt

Hans,

I do have a v2 and will check my github code and get back to you. I haven't tested the code on that device yet.

POSTED BY: BoB LeSuer

Hans,

I have not done an exhaustive check, but it looks like my code runs out of the box on the v2 model of the RPi. There are, however, some typos in my instructions. Here's the process I followed, so please tell me where things go wrong.

clone the repository. I made a directory called ~/test/ and put it there.

git clone git://github.com/bobthechemist/wiringPiLink.git

Now, cd ~/test/wiringPiLink and make two directories

mkdir wiringPiLink/LibraryResources
mkdir wiringPiLink/LibraryResources/Linux-ARM

I am fairly new to software distribution, and this is clearly not the best implementation. Learning how to distribute my projects more effectively is on the list of things to do...

Compile the library and move it to the directory you just created

~/test/wiringPiLink/compile.sh
cp ~/test/wiringPiLink/libwiringpi.so.1.0.2 wiringPiLink/LibraryResources/Linux-ARM

Create a simple circuit with an LED and resistor. I have connected the circuit's ground to the RPi's ground and the circuit's positive to the RPi pin 16 (GPIO 23 or in the wiring pi definition, pin 4). Note that my software currently only recognizes the wiringPi definition. Load up wolfram (don't forget to sudo)

sudo wolfram

Now you should be able to load the library and use it

<<wiringPiLink`
wiringPiMode[4,1]
wiringPiWrite[4,1]
wiringPiWrite[4,0]
wiringPiBounce[4]

Please let me know where things go wrong so I can fix the bugs and send a new commit to github. Thanks for trying out the code

POSTED BY: BoB LeSuer

This is amazing Bob. I'll definitely give it a try! Here is the bouncing ball effect from your demo:

enter image description here

POSTED BY: Bernat Espigulé
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract