# Raspberry Pi, pigpio, Jupyter

**URL:** https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426
**Category:** New to Julia
**Created:** [April 23, 2019, 3:24am UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426 "2019-04-23T03:24:05Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [April 23, 2019, 3:24am UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426/1 "2019-04-23T03:24:05Z")

</div>

I’m exploring the use of Julia for controlling hardware in near-real-time fashion (for prototyping, can tolerate some occasional hiccups). I tried the PiGPIO.jl package, but it’s a bit slow (hundreds of microseconds per GPIO update), so I used ccall to call functions in libpigpio directly, which brings the speed up just a bit, to around 100 ns per gpio write! A few questions arose along the way:

I have to run Julia as root in order to use libpigpio, but jupyter isn’t happy running as root. Anyone solved this before?

Has someone done a package for libpigpio (for direct C function access)? If not, I may try to write a package!

---

<div class="post-metadata">

### Author: ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)
#### Post date: [April 23, 2019, 10:48am UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426/2 "2019-04-23T10:48:30Z")

</div>

I will have to set up my Raspberry Pi or my Jetson Nano.  
Are you sure that you need to run as root to use libpigpio?  
It looks like the pigpio daemon needs to run as root. Does the user code have to also?  
Can you not change the permissions on the relevant devices? Forgive my ignorance of GPIO.

This discussion also might be of use:  
[https://raspberrypi.stackexchange.com/questions/60240/what-sets-ownership-and-privileges-on-dev-gpiomem](https://raspberrypi.stackexchange.com/questions/60240/what-sets-ownership-and-privileges-on-dev-gpiomem)

You need to be in the gpio group. Send us the permissions/ownership of /dev/gpiomem please?  
As I say - I dont have an RPi system up and running.

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [April 23, 2019, 11:10am UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426/3 "2019-04-23T11:10:22Z")

</div>

> [@klaff](#):
>
> I tried the PiGPIO.jl package, but it’s a bit slow

Maybe you can profile what is slow and fix it or open an issue to get some insides?  
I don’t see any reason, why Julia shouldn’t reach the same performance, and seems like PiGPIO.jl is a pretty compact package, so it may be easy to fix.

---

<div class="post-metadata">

### Author: ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)
#### Post date: [April 23, 2019, 11:13am UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426/4 "2019-04-23T11:13:55Z")

</div>

Perhaps not relevant. There was an excellent presentation on Julia Robotics at JuliaCon 2018  
As I remember the inner control loop executes in about a microsecond, in order to keep the robot upright. I have no idea what hardware was involved with that robot, but I doubt they would tolerate long delays.  
Maybe following this group will give you some ideas:

> **[JuliaCon 2018](https://juliacon.org/2018/talks_workshops/128/)**
>
> JuliaRobotics: Making robots walk with Julia — JuliaCon 2018, London, UK

---

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [April 23, 2019, 11:43am UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426/5 "2019-04-23T11:43:31Z")

</div>

I can test again in a few days, but using Julia via PiGPIO.jl, which interfaces via sockets, took several hundred microseconds per gpio write. Using Julia and ccall to libpigpio was mostly 100 ns per write. So just to be clear, in both cases I was using Julia at the REPL.

---

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [April 23, 2019, 11:45am UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426/6 "2019-04-23T11:45:55Z")

</div>

Thanks, I’ll look into the groups setup. I didn’t change anything about that from how Raspbian was out of the box.

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [April 23, 2019, 12:46pm UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426/7 "2019-04-23T12:46:34Z")

</div>

> Using Julia and ccall to libpigpio was mostly 100 ns per write. So just to be clear, in both cases I was using Julia at the REPL.

Yeah I got that, but ccall’ing will use a C implementation, while `PiGPIO.jl` seems to be a pure Julia implementation, which I suspect could be equally fast.

---

<div class="post-metadata">

### Author: ![elsuizo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elsuizo/32/89_2.png) [@elsuizo](https://discourse.julialang.org/u/elsuizo)
#### Post date: [April 23, 2019, 1:03pm UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426/8 "2019-04-23T13:03:32Z")

</div>

You could add the “real time” patch kernel for better latency

> **[Real-time Kernel (Preempt-RT) for Raspberry Pi](https://hackaday.io/project/123415-real-time-kernel-preempt-rt-for-raspberry-pi)**
>
> Latency of the Kernel of a Raspberry Pi could be a problem, if you need e.g. to sample sensors with a fast and constant sample rate and your application is not allowed to lose samples, or to sample a measurement more than once. Usually, to guarantee...

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [April 23, 2019, 1:08pm UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426/9 "2019-04-23T13:08:09Z")

</div>

Thanks for the shout out for the robotics talk 🙂

Unfortunately, it may not be as relevant as you’d hope: the control loop we were working with for that project was around 1 _milli_second: For a walking robot, our control loop is quite a bit higher-level than just GPIO, as that 1 millisecond includes things like setting up and solving an optimization problem to choose the robot’s joint torques.

I do agree that it should be possible to match C performance for raw GPIO, and I think a lot of the lessons we learned from the work shown in that talk would be relevant (careful attention to memory allocation, lots of BenchmarkTools and ProfileView, etc.).

---

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [April 23, 2019, 3:50pm UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426/10 "2019-04-23T15:50:16Z")

</div>

Thanks for the pointer - it’s good to know that’s there.

---

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [April 23, 2019, 4:18pm UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426/11 "2019-04-23T16:18:29Z")

</div>

I just read the “Robot Locomotion” case study. Awesome stuff. I’m not making robots but am working on embedded systems that use control loops running in your range of speeds (e.g. a few hundred per second) and am exploring options for prototyping. Writing/compiling/testing with C on a microcontroller is the old standard, but requires specialized environment and is slow (development-wise); Arduinos or pyboards let you move a lot faster, but have memory and speed limits and moving data on/off them is somewhat clunky. Julia on a Raspberry Pi is really a tantalizing option. The RT kernel is interesting and I’m also curious about how much benefit (if any) one gets from controlling CPU affinities. Lot’s of playing to do. Thanks!

---

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [April 23, 2019, 5:02pm UTC](https://discourse.julialang.org/t/raspberry-pi-pigpio-jupyter/23426/12 "2019-04-23T17:02:52Z")

</div>

My understanding of PiGPIO.jl is that it is pure Julia, but it’s communicating with pigpiod over sockets, such that you could be on different machines with different operating systems. That’s really neat, but I don’t need remote access and it means each access involves a bunch of networking stuff with wrappers and addresses and maybe interrupts (I don’t know anything about the network stack especially how localhost is handled). In contrast, ccall access to the libpigpio.so involves about 3 ARM instructions per gpioWrite call, the function call overhead, the bounds checking in gpioWrite, a single register write and the return from the function.
