Log in

View Full Version : How to use C with a Raspberry Pi servo HAT?


Ngang
26-07-2016, 11:29
Hi, I've been working on a project not entirely FRC related but I was hoping someone would have some advice.

It's my first time working with Raspberry Pi, but my team wants to try and use them and ROS to control a large group of robots with C, but it doesn't seem like C is really supported by that many libraries with the HAT. Are there other programming languages more viable for this? Does anyone have any experience with the HAT and C?

Andrew Lobos
26-07-2016, 11:39
I ported the Adafruit servo HAT library to C++ for a personal project a while ago. Worked great for driving a few victors on a small robot. This is the HAT I used: https://www.adafruit.com/products/2327

Here's the source: https://github.com/4ndr3w/PiBot/blob/e81e369494136ceea5d1d556b484e47e470a2e11/drivers/PWM.cpp

https://github.com/4ndr3w/PiBot/blob/e81e369494136ceea5d1d556b484e47e470a2e11/drivers/PWM.h

You'll need to link it with the WiringPI library.

Also be very careful if you're driving a large robot like this - if your program crashes/exits, the servo hat will keep outputting the last PWM value it was set at.

Ngang
26-07-2016, 11:58
Thanks!