1 #include "hardwareinterface/AdafruitPWMServoHat.h" 10 m_i2c = wiringPiI2CSetup(PCA9685_ADDRESS);
12 wiringPiI2CWriteReg8(m_i2c, MODE2, OUTDRV);
13 wiringPiI2CWriteReg8(m_i2c, MODE1, ALLCALL);
14 std::this_thread::sleep_for(std::chrono::milliseconds(5));
15 int mode1 = wiringPiI2CReadReg8(m_i2c, MODE1);
16 mode1 = mode1 & ~SLEEP;
17 wiringPiI2CWriteReg8(m_i2c, MODE1, mode1);
18 std::this_thread::sleep_for(std::chrono::milliseconds(5));
20 this->setPwmFrequency((
double)m_hz);
29 speed = (speed > 4095) ? 4095 : (speed < 0) ? 0 : speed;
30 this->
setPwm(motorID, 0, speed);
42 for (
int i = 0; i < 16; i++)
49 void AdafruitPWMServoHat::softwareReset()
51 wiringPiI2CWrite(m_i2c, 0x06);
54 void AdafruitPWMServoHat::setPwmFrequency(
double freqHz)
56 double prescaleval = 25000000.0;
57 prescaleval /= 4096.0;
58 prescaleval /= freqHz;
60 std::cout <<
"Setting PWM Frequency to " << freqHz <<
"Hz" << std::endl;
61 std::cout <<
"Estimated Prescale: " << prescaleval << std::endl;
62 int prescale = (int)floor(prescaleval + 0.5);
63 std::cout <<
"Final Prescale: " << prescale << std::endl;
64 int oldmode = wiringPiI2CReadReg8(m_i2c, MODE1);
65 int newmode = (oldmode & 0x7F) | 0x10;
66 wiringPiI2CWriteReg8(m_i2c, MODE1, newmode);
67 wiringPiI2CWriteReg8(m_i2c, PRESCALE, prescale);
68 wiringPiI2CWriteReg8(m_i2c, MODE1, oldmode);
69 std::this_thread::sleep_for(std::chrono::milliseconds(5));
70 wiringPiI2CWriteReg8(m_i2c, MODE1, oldmode | 0x80);
75 wiringPiI2CWriteReg8(m_i2c, LED0_ON_L + 4*channel, on & 0xFF);
76 wiringPiI2CWriteReg8(m_i2c, LED0_ON_H + 4*channel, on >> 8);
77 wiringPiI2CWriteReg8(m_i2c, LED0_OFF_L + 4*channel, off & 0xFF);
78 wiringPiI2CWriteReg8(m_i2c, LED0_OFF_H + 4*channel, off >> 8);
83 wiringPiI2CWriteReg8(m_i2c, ALL_LED_ON_L, on & 0xFF);
84 wiringPiI2CWriteReg8(m_i2c, ALL_LED_ON_H, on >> 8);
85 wiringPiI2CWriteReg8(m_i2c, ALL_LED_OFF_L, off & 0xFF);
86 wiringPiI2CWriteReg8(m_i2c, ALL_LED_OFF_H, off >> 8);
void setAllPwm(int on, int off)
bool setMotor(int motorID, int speed)
bool stopMotor(int motorID)
AdafruitPWMServoHat(int pwmFrequency=60)
void setPwm(int channel, int on, int off)