Introduction
On the D-Robotics RDK X5, the Ubuntu libwiringpi2 package (Raspberry Pi WiringPi) does not work:
You need to install the WiringPi fork for X5 (Horizon / D-Robotics), then compile a small program that generates 50 Hz pulses with a width of 600–2400 µs (standard hobby servo).
Prerequisites
sudo apt-get update
sudo apt-get install -y git build-essential g++
1. Installing WiringPi (RDK X5 Fork)
sudo git clone https://gitee.com/xin03liu/WiringPi.git /opt/wiringpi-rdk
cd /opt/wiringpi-rdk
sudo git checkout --detach 59cc2e82c7a92d2e4ce5bb8e732ebbfe5512ec3f
sudo ./build
sudo ldconfig
Verification:
ldconfig -p | grep wiringPi
# Expected: libwiringPi.so in /usr/local/lib
ls /usr/local/include/wiringPi.h
Note:
gpio readallmight fail (undefined symbol: pwmSetFreq) this does not affect our program.
2. Code Example softPWM.cpp
Create the file (e.g., in /app/40pin_samples/ or ~/softPWM.cpp):
// softPWM.cpp — 50 Hz Servo, 600–2400 µs sweep
// Build: see section 3
#include <wiringPi.h>
#include <cerrno>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <ctime>
static const int SERVO_HZ = 50;
static const long SERVO_PERIOD_NS = 1000000000L / SERVO_HZ;
static const int PULSE_MIN_US = 600;
static const int PULSE_MAX_US = 2400;
static const int SWEEP_STEP_US = 10;
static volatile sig_atomic_t stopping = 0;
static int g_pin = 29;
static int64_t mono_ns(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (int64_t)ts.tv_sec * 1000000000LL + ts.tv_nsec;
}
static void on_sig(int sig)
{
(void)sig;
stopping = 1;
}
static void sleep_until(const struct timespec *deadline)
{
while (!stopping &&
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, deadline, NULL) == EINTR) {
}
}
static void add_ns(struct timespec *ts, long ns)
{
ts->tv_nsec += ns;
while (ts->tv_nsec >= 1000000000L) {
ts->tv_sec++;
ts->tv_nsec -= 1000000000L;
}
}
static void emit_servo_pulse(int physical, int pulse_us)
{
int64_t width_ns, tend, t;
if (pulse_us < PULSE_MIN_US) pulse_us = PULSE_MIN_US;
if (pulse_us > PULSE_MAX_US) pulse_us = PULSE_MAX_US;
width_ns = (int64_t)pulse_us * 1000LL;
digitalWrite(physical, HIGH);
tend = mono_ns() + width_ns;
do { t = mono_ns(); } while (!stopping && t < tend);
digitalWrite(physical, LOW);
}
int main(int argc, char **argv)
{
int pin = 29;
int pulse_us = PULSE_MIN_US;
int direction = 1;
struct timespec cycle;
if (argc >= 2) {
pin = atoi(argv[1]);
if (pin <= 0) {
fprintf(stderr, "Invalid BOARD pin: %s\n", argv[1]);
return 1;
}
}
g_pin = pin;
signal(SIGINT, on_sig);
signal(SIGTERM, on_sig);
if (wiringPiSetupPhys() == -1) {
fprintf(stderr,
"wiringPiSetupPhys() failed.\n"
"Install the RDK X5 WiringPi fork (section 1), not the standard Pi wiringPi.\n");
return 1;
}
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
printf("Servo sweep BOARD pin %d: %d-%d us @ %d Hz — Press Ctrl+C to stop\n",
pin, PULSE_MIN_US, PULSE_MAX_US, SERVO_HZ);
clock_gettime(CLOCK_MONOTONIC, &cycle);
while (!stopping) {
emit_servo_pulse(pin, pulse_us);
pulse_us += direction * SWEEP_STEP_US;
if (pulse_us >= PULSE_MAX_US) { pulse_us = PULSE_MAX_US; direction = -1; }
else if (pulse_us <= PULSE_MIN_US) { pulse_us = PULSE_MIN_US; direction = 1; }
add_ns(&cycle, SERVO_PERIOD_NS);
sleep_until(&cycle);
/* If CPU falls behind: reset to now (prevent pulse bursts) */
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if (now.tv_sec > cycle.tv_sec ||
(now.tv_sec == cycle.tv_sec &&
now.tv_nsec > cycle.tv_nsec + SERVO_PERIOD_NS)) {
cycle = now;
}
}
digitalWrite(g_pin, LOW);
printf("\nStopped — pin %d set to LOW\n", g_pin);
return 0;
}
3. Compilation
cd /path/to/softPWM.cpp
g++ -O2 -o softPWM softPWM.cpp \
-lwiringPi -lrt -lpthread \
-I/usr/local/include \
-L/usr/local/lib \
-Wl,-rpath,/usr/local/lib
4. Running the Program
Connect the servo signal wire to the chosen BOARD pin (e.g. pin 29), with VCC and GND connected to a dedicated power supply (5 V recommended for standard servos with sufficient current).
sudo ./softPWM 29
- Optional argument: BOARD pin number (default is 29).
- The servo continuously sweeps from 600 → 2400 → 600 µs.
- Press Ctrl+C to exit (the pin will be reset to LOW).
Fixed position (no sweep): keep pulse_us constant in the loop, or call emit_servo_pulse(pin, 1500) inside a 50 Hz loop.
5. References
- Tested WiringPi fork:
gitee.com/xin03liu/WiringPi@59cc2e82