Hi All -
I hope you can help me with this. I have some code that works fine on the core2, but does not on the core2mini.
Specifically, the motors do not reverse on the core2mini, while they do on the core2. For example, to test this, I set up some code that turns the motor one direction when the ‘A’ key is pressed, it turns the other way when the ‘S’ key is pressed, and it stops when the ‘Z’ key is pressed. (The other motor reacts to the ‘J’, ‘K’ and ‘M’ keys respectively.)
Also, on the core2, the motor stops when it has moved the desired amount, while on the core2mini, the motor doesn’t stop until I press the ‘Z’ key.
On the core2mini, however, pressing the ‘A’ and ‘S’ keys, the motor turns the same direction. Can someone tell me why this might be?
The hardware setup is the core2mini on a RPi3 with two motors w/ encoder attached.
Here is the code:
#include "hFramework.h"
#include "hCloudClient.h"
void cfgHandler()
{
platform.ui.loadHtml({Resource::URL, "/ui.html"});
}
hPIDRegulator myRegA;
hPIDRegulator myRegB;
void setupMotorPID()
{
float kp = 40.0;
float ki = 0.05;
float kd = 1000.0;
// hPID
myRegA.setScale(1);
myRegA.setKP(kp);
myRegA.setKI(ki);
myRegA.setKD(kd);
myRegB.setScale(1);
myRegB.setKP(kp);
myRegB.setKI(ki);
myRegB.setKD(kd);
// hRegulator
myRegA.dtMs = 5;
myRegA.stableRange = 10;
myRegA.stableTimes = 3;
myRegB.dtMs = 5;
myRegB.stableRange = 10;
myRegB.stableTimes = 3;
hMotA.attachPositionRegulator(myRegA);
hMotA.resetEncoderCnt();
hMotB.attachPositionRegulator(myRegB);
hMotB.resetEncoderCnt();
}
void encCnt()
{
for(;;)
{
platform.ui.label("lb_enc_a").setText("%d", (int)hMotA.getEncoderCnt());
platform.ui.label("lb_enc_b").setText("%d", (int)hMotB.getEncoderCnt());
platform.ui.label("l1").setText("uptime %u", (unsigned int)sys.getRefTime());
uint16_t v_int = hSens1.pin1.analogReadRaw();
platform.ui.label("lb_sensor").setText("%d", v_int);
sys.delay(100);
}
}
void cloudTask() {
platform.begin(&RPi);
hMotA.setEncoderPolarity(Polarity::Reversed);
hMotB.setEncoderPolarity(Polarity::Reversed);
platform.ui.setProjectId("@@@PROJECT_ID@@@");
platform.ui.configHandler = cfgHandler;
platform.ui.onKeyEvent = [](KeyEventType type, KeyCode code) {
if (type == KeyEventType::Pressed) {
if (code == KeyCode::Key_A) {
hMotA.rotRel(3840, 200);
}
if (code == KeyCode::Key_S) {
hMotA.rotRel(-3840, 200);
}
if (code == KeyCode::Key_Z) {
hMotA.rotRel(0, 0);
}
if (code == KeyCode::Key_J) {
hMotB.rotRel(3840, 200);
}
if (code == KeyCode::Key_K) {
hMotB.rotRel(-3840, 200);
}
if (code == KeyCode::Key_M) {
hMotB.rotRel(0, 0);
}
}
};
while (1) {
sys.delay(500);
LED3.toggle();
}
}
void hMain()
{
sys.taskCreate(cloudTask);
sys.taskCreate(encCnt);
setupMotorPID();
while (1) {
LED1.toggle();
}
}
Thanks in advance for your help.