Skip to content

Commit 5811e1b

Browse files
committed
Add examples for upcoming NanoMotorCarrier
1 parent 5187891 commit 5811e1b

File tree

7 files changed

+1543
-0
lines changed

7 files changed

+1543
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <ArduinoMotorCarrier.h>
2+
#define INTERRUPT_PIN 6
3+
4+
//Variable to store the battery voltage
5+
static int batteryVoltage;
6+
7+
//Variable to change the motor speed and direction
8+
static int duty = 0;
9+
10+
void setup()
11+
{
12+
//Serial port initialization
13+
Serial.begin(115200);
14+
//while (!Serial);
15+
16+
//Establishing the communication with the motor shield
17+
if (controller.begin())
18+
{
19+
Serial.print("MKR Motor Shield connected, firmware version ");
20+
Serial.println(controller.getFWVersion());
21+
}
22+
else
23+
{
24+
Serial.println("Couldn't connect! Is the red led blinking? You may need to update the firmware with FWUpdater sketch");
25+
while (1);
26+
}
27+
28+
// Reboot the motor controller; brings every value back to default
29+
Serial.println("reboot");
30+
controller.reboot();
31+
delay(500);
32+
33+
int dutyInit = 0; // at 50 it works as espected, at 60 shift sides and is too small duty to move, at 70 is very big duty.
34+
M1.setDuty(dutyInit);
35+
M2.setDuty(dutyInit);
36+
M3.setDuty(dutyInit);
37+
M4.setDuty(dutyInit);
38+
Serial.print("Duty init: ");
39+
Serial.println(dutyInit);
40+
// int duty2 = dutyInit * 16777215 / 100;
41+
// Serial.print("Conversion formula: ");
42+
// Serial.println(duty2);
43+
//while (1); //WHILE 1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! REMOVE!!!!
44+
}
45+
46+
47+
void loop() {
48+
49+
//Take the battery status
50+
//float batteryVoltage = (float)battery.getConverted();
51+
52+
//Reset to the default values if the battery level is lower than 11V
53+
// if (batteryVoltage < 11)
54+
// {
55+
// Serial.println(" ");
56+
// Serial.println("WARNING: LOW BATTERY");
57+
// Serial.println("ALL SYSTEMS DOWN");
58+
// M1.setDuty(0);
59+
// M2.setDuty(0);
60+
// M3.setDuty(0);
61+
// M4.setDuty(0);
62+
// while (batteryVoltage < 11)
63+
// {
64+
// batteryVoltage = (float)battery.getConverted();
65+
// }
66+
// }
67+
// else
68+
// {
69+
70+
//Motor test
71+
for (duty = -100; duty < 100; duty += 1)
72+
{
73+
Serial.print("Motor Duty: ");
74+
Serial.println(duty);
75+
M1.setDuty(duty);
76+
M2.setDuty(duty);
77+
M3.setDuty(duty);
78+
M4.setDuty(duty);
79+
delay(10);
80+
}
81+
for (duty = 100; duty > -100; duty -= 1)
82+
{
83+
Serial.print("Motor Duty: ");
84+
Serial.println(duty);
85+
M1.setDuty(duty);
86+
M2.setDuty(duty);
87+
M3.setDuty(duty);
88+
M4.setDuty(duty);
89+
delay(10);
90+
}
91+
92+
//Keep active the communication MKR1000 & MKRMotorCarrier
93+
//Ping the samd11
94+
controller.ping();
95+
//wait
96+
delay(50);
97+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <ArduinoMotorCarrier.h>
2+
#define INTERRUPT_PIN 6
3+
4+
//Variable to store the battery voltage
5+
static int batteryVoltage;
6+
7+
//Variable to change the motor speed and direction
8+
static int duty = 0;
9+
10+
void setup()
11+
{
12+
//Establishing the communication with the motor shield
13+
if (controller.begin())
14+
{
15+
Serial.print("MKR Motor Shield connected, firmware version ");
16+
Serial.println(controller.getFWVersion());
17+
}
18+
else
19+
{
20+
Serial.println("Couldn't connect! Is the red led blinking? You may need to update the firmware with FWUpdater sketch");
21+
while (1);
22+
}
23+
24+
//Serial port initialization
25+
Serial.begin(115200);
26+
while (!Serial);
27+
28+
// Reboot the motor controller; brings every value back to default
29+
Serial.println("reboot");
30+
controller.reboot();
31+
delay(500);
32+
33+
// Reset the encoder internal counter to zero (can be set to any initial value)
34+
Serial.println("reset counters");
35+
encoder1.resetCounter(0);
36+
encoder2.resetCounter(0);
37+
38+
M1.setDuty(30);
39+
M2.setDuty(30);
40+
M3.setDuty(30);
41+
M4.setDuty(30);
42+
}
43+
44+
45+
void loop() {
46+
47+
//Chose the encoder to use:encoder1(default) or encoder2
48+
Serial.print("Encoder1 Pos [counts]: ");
49+
Serial.print(encoder1.getRawCount());
50+
Serial.print(" Encoder1 vel [counts/sec]: ");
51+
Serial.println(encoder1.getCountPerSecond());
52+
Serial.print("Encoder2 Pos [counts]: ");
53+
Serial.print(encoder2.getRawCount());
54+
Serial.print(" Encoder2 vel [counts/sec]: ");
55+
Serial.println(encoder2.getCountPerSecond());
56+
Serial.println("");
57+
58+
//Keep active the communication MKR1000 & MKRMotorCarrier
59+
//Ping the samd11
60+
controller.ping();
61+
//wait
62+
delay(50);
63+
}

examples/Nano/Flasher/Flasher.ino

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
STANDALONE FIRMWARE UPDATE FOR MKR Motor Shiuld
3+
4+
To generate a new firmware, compile D11-Firmware with target MKRMotorShield, 4KB bootloader, LTO enabled, pinmap complete
5+
and execute
6+
7+
echo -n "const " > fw.h && xxd -i D11-Firmware.ino.bin >> fw.h
8+
*/
9+
10+
#include "Wire.h"
11+
#include "fw.h"
12+
#include "MKRMotorCarrier.h"
13+
14+
#define I2C_ADDRESS 0x09
15+
16+
void setDataRunning(int cmd, uint8_t target, int data) {
17+
Wire.beginTransmission(0x66);
18+
Wire.write((uint8_t)cmd);
19+
Wire.write((uint8_t)target);
20+
Wire.write(data);
21+
Wire.endTransmission();
22+
}
23+
24+
25+
void setup() {
26+
// put your setup code here, to run once:
27+
Wire.begin();
28+
Wire.setClock(1000000);
29+
30+
Serial.begin(115200);
31+
while (!Serial);
32+
pinMode(6, OUTPUT);
33+
digitalWrite(6, HIGH);
34+
35+
int prev_address = -1;
36+
37+
Wire.beginTransmission(0x66);
38+
Wire.write((uint8_t)GET_VERSION);
39+
Wire.write((uint8_t)0);
40+
Wire.endTransmission();
41+
42+
Wire.requestFrom(0x66, 5);
43+
Wire.read();
44+
45+
String version = "";
46+
47+
while (Wire.available()) {
48+
version += (char)Wire.read();
49+
}
50+
51+
if (version.c_str()[0] == '0') {
52+
Serial.println("Reset D11");
53+
setDataRunning(RESET, 0, 0);
54+
delay(10);
55+
}
56+
57+
// reset running D11
58+
Serial.println("Erase flash");
59+
60+
Wire.beginTransmission(I2C_ADDRESS);
61+
Wire.write('r');
62+
Wire.endTransmission();
63+
64+
delay(500);
65+
66+
Serial.println("Starting flash");
67+
68+
int address = 0;
69+
while (address < (D11_Firmware_ino_bin_len + 0x1000)) {
70+
int retry = 0;
71+
do {
72+
Wire.requestFrom(I2C_ADDRESS, 4);
73+
uint8_t buf[4];
74+
int k = 0;
75+
while (Wire.available()) {
76+
buf[k++] = Wire.read();
77+
}
78+
address = *(uint32_t*)buf;
79+
delay(10);
80+
} while (prev_address == address && retry++ < 5);
81+
prev_address = address;
82+
Serial.println(address);
83+
84+
uint8_t crc = 0;
85+
for (int j = 0; j < 64; j++) {
86+
crc ^= D11_Firmware_ino_bin[address - 0x1000 + j];
87+
}
88+
89+
Serial.println(crc, HEX);
90+
91+
Wire.beginTransmission(I2C_ADDRESS);
92+
Wire.write('w');
93+
Wire.write(crc);
94+
Wire.write(&D11_Firmware_ino_bin[address - 0x1000], 64);
95+
Wire.endTransmission();
96+
}
97+
98+
Serial.println("Booting FW");
99+
Wire.beginTransmission(I2C_ADDRESS);
100+
Wire.write('x');
101+
Wire.endTransmission();
102+
103+
delay(1000);
104+
105+
Wire.beginTransmission(0x66);
106+
Wire.write((uint8_t)GET_VERSION);
107+
Wire.write((uint32_t)0);
108+
Wire.endTransmission();
109+
110+
Wire.requestFrom(0x66, 5);
111+
Wire.read();
112+
113+
Serial.print("New version: ");
114+
while (Wire.available()) {
115+
Serial.print((char)Wire.read());
116+
}
117+
Serial.println();
118+
}
119+
120+
void loop() {
121+
// put your main code here, to run repeatedly:
122+
123+
}

0 commit comments

Comments
 (0)