Hey there!
Any one out there written a C or C++ driver for the explorer pHat? Or can anyone from the crew point me in the right direction to get something like this off the ground quicker?
Happy to re-post code after!
Cheers
Alex
Hey there!
Any one out there written a C or C++ driver for the explorer pHat? Or can anyone from the crew point me in the right direction to get something like this off the ground quicker?
Happy to re-post code after!
Cheers
Alex
Got bored - here’s a quick implementation of a motion controller using the Explorer pHat in C++, the same logic can be applied to other pins on the board:
class MotionController
{
enum PimoroniMotorPins
{
M1B = 19,
M1F = 20,
M2B = 21,
M2F = 26
};
template<int PIN_F, int PIN_B>
class Motor
{
enum { kForwardPin = PIN_F, kBackwardPin = PIN_B };
bool m_initialized;
public:
Motor()
: m_initialized(false)
{}
bool init()
{
int result = wiringPiSetupGpio();
if (result != 0)
{
throw std::runtime_error("Failed to init wiring pi");
return false;
}
pinMode(kForwardPin, OUTPUT);
result = softPwmCreate(kForwardPin, 0, 100);
if (result != 0)
{
throw std::runtime_error("Failed to create SW PWM pin");
return false;
}
pinMode(kBackwardPin, OUTPUT);
result = softPwmCreate(kBackwardPin, 0, 100);
if (result != 0)
{
throw std::runtime_error("Failed to create SW PWM pin");
return false;
}
m_initialized = true;
return true;
}
void setSpeed(float speed)
{
if (!m_initialized) return;
int fwdCycle = (int)(saturate( speed) * 100 + 0.5f);
int backCycle = (int)(saturate(-speed) * 100 + 0.5f);
softPwmWrite(kForwardPin, fwdCycle);
softPwmWrite(kBackwardPin, backCycle);
}
};
Motor<M1F, M1B> m_motorLeft;
Motor<M2F, M2B> m_motorRight;
public:
enum MotorSide
{
kLeft = 1,
kRight = 2,
kNumMotors
};
bool init()
{
if (!m_motorLeft.init() || !m_motorRight.init())
{
return false;
}
return true;
}
void setSpeed(const MotorSide motor, float speed)
{
switch (motor)
{
case kRight:
{
m_motorRight.setSpeed(speed);
break;
}
case kLeft:
{
m_motorLeft.setSpeed(speed);
break;
}
}
}
};
Thanks for starting this. A year later it’s still getting hits.
I am compiling it on a Pi with GCC with a bunch of includes and one bodge as follows:
#include <wiringPi.h>
#include <softPwm.h>
#include
#include
#define saturate(x) x
I’m not able to get anywhere further though. could you show an example of using the class?