Trouble with servo using ofxGPIO


#1

I’m having trouble with the ofxGPIO example. I got the blinking LED sketch to run. The servo sketch compiles and runs, spits out console info, but doesn’t turn the servo.

#include "ofAppNoWindow.h"
#include "ofMain.h"
#include "ofxGPIO.h"

class noWin : public ofBaseApp {
public:
  rpiPWM1 *pwm;
  int count;

  void setup() {
    pwm = new rpiPWM1(1000.0, 256, 80.0, rpiPWM1::MSMODE);
    count = 0;
  }

  void update() {
    pwm->setDutyCycleCount(count);
    count += 16;
    if (count > 100)
      count = 0;
    printf("Duty Cycle is %3.2lf \n", pwm->getDutyCycle());
    printf("Divisor is %d\n", pwm->getDivisor());
    usleep(2000000);
  }

  void exit() {}
};

int main() {
  ofAppNoWindow window;
  ofSetupOpenGL(&window, 1024, 768, OF_WINDOW);
  ofRunApp(new noWin());
}

I did some digging and found that rpiPWM1 is GPIO18, which aligns with the rest of what I’ve read (that is one of the only PWM pins). I did get this python program to run and properly turn my servo:

import RPi.GPIO as GPIO
import time

servoPIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)

p = GPIO.PWM(servoPIN, 50) # GPIO 18 for PWM with 50Hz
p.start(2.5) # Initialization
try:
  while True:
    p.ChangeDutyCycle(5)
    time.sleep(0.5)
    p.ChangeDutyCycle(7.5)
    time.sleep(0.5)
    p.ChangeDutyCycle(10)
    time.sleep(0.5)
    p.ChangeDutyCycle(12.5)
    time.sleep(0.5)
    p.ChangeDutyCycle(10)
    time.sleep(0.5)
    p.ChangeDutyCycle(7.5)
    time.sleep(0.5)
    p.ChangeDutyCycle(5)
    time.sleep(0.5)
    p.ChangeDutyCycle(2.5)
    time.sleep(0.5)
except KeyboardInterrupt:
  p.stop()
  GPIO.cleanup()

I noticed that the Hz specified in the python program is far lower than that of the ofxGPIO example, so I changed that value to 50 but no change.

Any help is appreciated.