Skip to content

Commit 4fa9bd3

Browse files
committed
Improve library usage and example
1 parent abe0e85 commit 4fa9bd3

File tree

6 files changed

+91
-63
lines changed

6 files changed

+91
-63
lines changed

examples/Test/Test.ino

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,51 @@
11
#include "MKRMotorShield.h"
22

3-
const int pingPin = IN4;
4-
const byte interruptPin = IN2;
5-
6-
long encoderResetValue = 5;
7-
8-
mc::MotorController controller;
9-
10-
mc::ServoMotor servo1 = mc::ServoMotor();
11-
mc::ServoMotor servo2 = mc::ServoMotor();
12-
mc::ServoMotor servo3 = mc::ServoMotor();
13-
mc::ServoMotor servo4 = mc::ServoMotor();
14-
15-
mc::DCMotor M2 = mc::DCMotor();
16-
mc::DCMotor M1 = mc::DCMotor();
17-
d21::DCMotor M3 = d21::DCMotor();
18-
d21::DCMotor M4 = d21::DCMotor();
19-
20-
mc::Encoder encoder2 = mc::Encoder();
21-
mc::Encoder encoder1 = mc::Encoder();
22-
23-
mc::PID pid2 = mc::PID();
24-
mc::PID pid1 = mc::PID();
25-
26-
volatile uint8_t irq_status;
27-
28-
void getDataIrq() {
29-
irq_status = 1;
30-
};
31-
32-
int m2_pos = 0;
33-
//boolean toggle1 = false;
34-
//boolean toggle2 = false;
35-
363
void setup() {
37-
4+
385
Serial.begin(115200);
396
while (!Serial);
40-
// create mkrshield connection
41-
Wire.begin();
42-
43-
delay(500);
447

45-
//create encoder2 object in MATLAB
46-
pinMode(IRQ_PIN, INPUT_PULLUP);
47-
attachInterrupt(IRQ_PIN, getDataIrq, FALLING);
48-
encoder2.resetCounter(0);
49-
50-
// create dcmotor2 object in matlab
51-
M2.setFrequency(100);
8+
// Start communicationg with the motor shield
9+
if (controller.begin()) {
10+
Serial.print("MKR Motor Shield connected, firmware version ");
11+
Serial.println(controller.getFWVersion());
12+
} else {
13+
Serial.println("Couldn't connect! Is the red led blinking? You may need to update the firmware with FWUpdater sketch");
14+
while (1);
15+
}
16+
17+
// Reboot the motor controller; brings every value back to default
18+
controller.reboot();
5219

53-
// enable dc motor2 object
20+
// Reset the encoder internal counter to zero (can be set to any initial value)
21+
encoder1.resetCounter(0);
5422

55-
M2.setDuty(90);
23+
// Start DC Motor 1 (labeled M1) with 90% of maximum speed, clockwise
24+
M1.setFrequency(100);
25+
M1.setDuty(90);
5626

57-
while(m2_pos < 36000){
58-
m2_pos = encoder2.getRawCount();
27+
// Read the encoder connected to Motor1 until it reaches 36000 counts
28+
int motor1Pos = 0;
29+
while (motor1Pos < 36000) {
30+
motor1Pos = encoder1.getRawCount();
31+
// Remember to call ping() here and there!
32+
// If you forget to do so the controller will think that the user sketch is not running and will freeze.
33+
// You can wait up to 3 seconds between pings before the reset kicks in
5934
controller.ping();
6035
}
61-
Serial.println(m2_pos);
62-
//M2.setDuty(0);
63-
pid2.setControlMode(CL_POSITION);
64-
pid2.setGains(25, 0, 3);
65-
pid2.setMaxAcceleration(4000);
66-
controller.ping();
67-
delay(1000);
68-
pid2.setSetpoint(TARGET_POSITION, 10000);
36+
37+
// Switch motor control from DIRECT to PID-driven.
38+
// This way, you can program the motor to reach a certain position or velocity without any further intervention.
39+
// The PID can be carefully tuned if a particular profile is needed.
40+
pid1.setControlMode(CL_POSITION);
41+
pid1.setGains(25, 0, 3);
42+
pid1.setMaxAcceleration(4000);
43+
pid1.setSetpoint(TARGET_POSITION, 5000);
6944
}
45+
7046
void loop() {
71-
m2_pos = encoder2.getRawCount();
72-
Serial.println(m2_pos);
47+
// Simply print the actual position while the PID is running, pinging the controller every loop()
48+
Serial.println(encoder1.getRawCount());
7349
controller.ping();
50+
delay(100);
7451
}

src/Common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ enum IRQCause {
4444
#define IN3 A5
4545
#define IN4 A2
4646

47+
namespace mc {
4748
int getData(Commands cmd, uint8_t target, uint8_t* buf);
4849
void setData(Commands cmd, uint8_t target, int data);
4950
void setDataPIDGains(Commands cmd, uint8_t target, int16_t P, int16_t I, int16_t D);
5051
int getData(Commands cmd, uint8_t* buf);
52+
}

src/MKRMotorShield.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "MKRMotorShield.h"
22

3+
namespace mc {
34
void setDataPIDGains(Commands cmd, uint8_t target, int16_t P, int16_t I, int16_t D) {
45
Wire.beginTransmission(I2C_ADDRESS);
56
Wire.write((uint8_t)cmd);
@@ -37,3 +38,25 @@ int getData(Commands cmd, uint8_t target, uint8_t* buf) {
3738
int getData(Commands cmd, uint8_t* buf) {
3839
return getData(cmd, 0, buf);
3940
}
41+
}
42+
43+
mc::MotorController controller;
44+
45+
mc::ServoMotor servo1;
46+
mc::ServoMotor servo2;
47+
mc::ServoMotor servo3;
48+
mc::ServoMotor servo4;
49+
50+
mc::DCMotor M1;
51+
mc::DCMotor M2;
52+
d21::DCMotor M3;
53+
d21::DCMotor M4;
54+
55+
mc::Encoder encoder1;
56+
mc::Encoder encoder2;
57+
58+
mc::PID pid1;
59+
mc::PID pid2;
60+
61+
mc::Battery battery;
62+

src/MKRMotorShield.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,22 @@
77
#include "Common.h"
88
#include "PID.h"
99

10-
void setDataPIDGains(Commands cmd, uint8_t target, int16_t P, int16_t I, int16_t D);
11-
void setData(Commands cmd, uint8_t target, int data);
12-
int getData(Commands cmd, uint8_t target, uint8_t* buf);
13-
int getData(Commands cmd, uint8_t* buf);
10+
extern mc::MotorController controller;
1411

12+
extern mc::ServoMotor servo1;
13+
extern mc::ServoMotor servo2;
14+
extern mc::ServoMotor servo3;
15+
extern mc::ServoMotor servo4;
16+
17+
extern mc::DCMotor M1;
18+
extern mc::DCMotor M2;
19+
extern d21::DCMotor M3;
20+
extern d21::DCMotor M4;
21+
22+
extern mc::Encoder encoder1;
23+
extern mc::Encoder encoder2;
24+
25+
extern mc::PID pid1;
26+
extern mc::PID pid2;
27+
28+
extern mc::Battery battery;

src/MotorController.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "MotorController.h"
22
#include "Common.h"
3+
#include "Wire.h"
34

45
String mc::MotorController::getFWVersion() {
56
char ret[5];
@@ -10,6 +11,7 @@ String mc::MotorController::getFWVersion() {
1011

1112
void mc::MotorController::reboot() {
1213
setData(RESET, 0, 0);
14+
delay(500);
1315
}
1416

1517
void mc::MotorController::ping() {
@@ -21,3 +23,12 @@ float mc::MotorController::getTemperature() {
2123
getData(GET_INTERNAL_TEMP, (uint8_t*)&ret);
2224
return (float)ret / 1000.0f;
2325
}
26+
27+
int mc::MotorController::begin() {
28+
Wire.begin();
29+
String version = getFWVersion();
30+
if (version.c_str()[0] == '0') {
31+
return 1;
32+
}
33+
return 0;
34+
};

src/MotorController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class MotorController {
1010
String getFWVersion();
1111
void reboot();
1212
void ping();
13+
int begin();
1314
float getTemperature();
1415
};
1516
}

0 commit comments

Comments
 (0)