Skip to content

Commit f02e357

Browse files
committed
first commit
0 parents  commit f02e357

40 files changed

+15863
-0
lines changed

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=MKRMotorShield
2+
version=0.0.1
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Allows to use the MKR Motor Shield
6+
paragraph=Allows to use the MKR MotorShield
7+
category=I/o
8+
url=http://www.arduino.cc/en/Reference/
9+
architectures=samd

src/Battery.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "Battery.h"
2+
#include "Common.h"
3+
4+
mc::Battery::Battery() {
5+
}
6+
7+
int mc::Battery::getRaw() {
8+
int ret;
9+
getData(GET_RAW_ADC_BATTERY, (uint8_t*)&ret);
10+
return ret;
11+
}
12+
13+
int mc::Battery::getConverted() {
14+
int ret;
15+
getData(GET_CONVERTED_ADC_BATTERY, (uint8_t*)&ret);
16+
return ret;
17+
}
18+
19+
int mc::Battery::getFiltered() {
20+
int ret;
21+
getData(GET_FILTERED_ADC_BATTERY, (uint8_t*)&ret);
22+
return ret;
23+
}

src/Battery.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "Arduino.h"
2+
3+
namespace mc {
4+
5+
class Battery {
6+
public:
7+
Battery();
8+
int getRaw();
9+
int getConverted();
10+
int getFiltered();
11+
};
12+
}

src/Common.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
enum Commands {
2+
GET_VERSION = 0x01,
3+
RESET,
4+
SET_PWM_DUTY_CYCLE_SERVO,
5+
SET_PWM_FREQUENCY_SERVO,
6+
SET_PWM_DUTY_CYCLE_DC_MOTOR,
7+
SET_PWM_FREQUENCY_DC_MOTOR,
8+
GET_RAW_COUNT_ENCODER,
9+
RESET_COUNT_ENCODER,
10+
GET_OVERFLOW_UNDERFLOW_STATUS_ENCODER,
11+
GET_COUNT_PER_SECOND_ENCODER,
12+
SET_INTERRUPT_ON_COUNT_ENCODER,
13+
SET_INTERRUPT_ON_VELOCITY_ENCODER,
14+
GET_RAW_ADC_BATTERY,
15+
GET_CONVERTED_ADC_BATTERY,
16+
GET_FILTERED_ADC_BATTERY,
17+
SET_PID_GAIN_CL_MOTOR,
18+
RESET_PID_GAIN_CL_MOTOR,
19+
SET_CONTROL_MODE_CL_MOTOR,
20+
SET_POSITION_SETPOINT_CL_MOTOR,
21+
SET_VELOCITY_SETPOINT_CL_MOTOR,
22+
SET_MAX_ACCELERATION_CL_MOTOR,
23+
SET_MAX_VELOCITY_CL_MOTOR,
24+
SET_MIN_MAX_DUTY_CYCLE_CL_MOTOR,
25+
PING,
26+
GET_INTERNAL_TEMP,
27+
};
28+
29+
enum IRQCause {
30+
ENCODER_COUNTER_REACHED = 1,
31+
ENCODER_VELOCITY_REACHED,
32+
};
33+
34+
#define I2C_ADDRESS 0x66
35+
#define IRQ_PIN 6
36+
37+
#define MOTOR_3_PIN_A 3
38+
#define MOTOR_3_PIN_B 2
39+
#define MOTOR_4_PIN_A 4
40+
#define MOTOR_4_PIN_B 5
41+
42+
#define IN1 A6
43+
#define IN2 A1
44+
#define IN3 A5
45+
#define IN4 A2
46+
47+
int getData(Commands cmd, uint8_t target, uint8_t* buf);
48+
void setData(Commands cmd, uint8_t target, int data);
49+
void setDataPIDGains(Commands cmd, uint8_t target, int16_t P, int16_t I, int16_t D);
50+
int getData(Commands cmd, uint8_t* buf);

src/DCMotor.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "DCMotor.h"
2+
#include "Common.h"
3+
4+
namespace mc {
5+
static int next_instance = 0;
6+
7+
DCMotor::DCMotor() {
8+
instance = next_instance;
9+
next_instance++;
10+
};
11+
12+
void DCMotor::setDuty(int duty) {
13+
setData(SET_PWM_DUTY_CYCLE_DC_MOTOR, instance, duty);
14+
}
15+
16+
void DCMotor::setFrequency(int frequency) {
17+
setData(SET_PWM_FREQUENCY_DC_MOTOR, instance, frequency);
18+
}
19+
20+
}
21+
// D21 implementations
22+
23+
namespace d21 {
24+
static int next_instance = 0;
25+
26+
DCMotor::DCMotor() {
27+
instance = next_instance;
28+
next_instance++;
29+
if (instance == 0) {
30+
in1 = MOTOR_3_PIN_A; //2;
31+
in2 = MOTOR_3_PIN_B; //3;
32+
} else {
33+
in1 = MOTOR_4_PIN_A; //5;
34+
in2 = MOTOR_4_PIN_B; //4;
35+
}
36+
pinMode(in1, OUTPUT);
37+
pinMode(in2, OUTPUT);
38+
};
39+
40+
void DCMotor::setDuty(int duty) {
41+
//setData(SET_PWM_DUTY_CYCLE_DC_MOTOR, instance, duty);
42+
this->duty = duty;
43+
if (duty == 0) {
44+
analogWrite(in1, 0);
45+
analogWrite(in2, 0);
46+
return;
47+
}
48+
if (duty > 0) {
49+
analogWrite(in1, 0);
50+
analogWrite(in2, map(duty, 0, 100, 0, 255));
51+
} else {
52+
analogWrite(in2, 0);
53+
analogWrite(in1, map(-duty, 0, 100, 0, 255));
54+
}
55+
56+
}
57+
58+
void DCMotor::setFrequency(int frequency) {
59+
//setData(SET_PWM_FREQUENCY_DC_MOTOR, instance, frequency);
60+
}
61+
}

src/DCMotor.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef __DCMOTOR_H__
2+
#define __DCMOTOR_H__
3+
4+
#include "Arduino.h"
5+
6+
namespace mc {
7+
8+
class DCMotor {
9+
public:
10+
DCMotor();
11+
void setDuty(int duty);
12+
void setFrequency(int frequency);
13+
private:
14+
int instance;
15+
};
16+
}
17+
18+
namespace d21 {
19+
20+
class DCMotor {
21+
public:
22+
DCMotor();
23+
void setDuty(int duty);
24+
void setFrequency(int frequency);
25+
private:
26+
int instance;
27+
int duty = 0;
28+
int in1;
29+
int in2;
30+
};
31+
}
32+
33+
#endif

src/Encoder.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "Encoder.h"
2+
#include "Wire.h"
3+
#include "Common.h"
4+
5+
static int next_instance = 0;
6+
7+
mc::Encoder::Encoder() {
8+
instance = next_instance;
9+
next_instance++;
10+
};
11+
12+
void mc::Encoder::resetCounter(long value) {
13+
setData(RESET_COUNT_ENCODER, instance, value);
14+
}
15+
16+
int mc::Encoder::getRawCount() {
17+
int ret;
18+
getData(GET_RAW_COUNT_ENCODER, instance, (uint8_t*)&ret);
19+
return ret;
20+
}
21+
22+
int mc::Encoder::getOverflowUnderflow() {
23+
uint8_t ret[2];
24+
getData(GET_OVERFLOW_UNDERFLOW_STATUS_ENCODER, instance, (uint8_t*)ret);
25+
return ret[0] << 8 | ret[1];
26+
}
27+
28+
int mc::Encoder::getCountPerSecond() {
29+
int ret;
30+
getData(GET_COUNT_PER_SECOND_ENCODER, instance, (uint8_t*)&ret);
31+
return ret;
32+
}
33+
34+
void mc::Encoder::setIrqOnCount(long value) {
35+
setData(SET_INTERRUPT_ON_COUNT_ENCODER, instance, value);
36+
}
37+
38+
void mc::Encoder::setIrqOnVelocity(long value, uint8_t margin) {
39+
setData(SET_INTERRUPT_ON_VELOCITY_ENCODER, instance, (margin << 24 | value));
40+
}

src/Encoder.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef __ENCODER_H__
2+
#define __ENCODER_H__
3+
4+
#include "Arduino.h"
5+
6+
namespace mc {
7+
8+
class Encoder {
9+
public:
10+
Encoder();
11+
int getRawCount();
12+
int getOverflowUnderflow();
13+
int getCountPerSecond();
14+
void resetCounter(long value);
15+
void setIrqOnCount(long value);
16+
void setIrqOnVelocity(long value, uint8_t margin = 2);
17+
private:
18+
int instance;
19+
};
20+
}
21+
22+
#endif

src/MKRMotorShield.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <Wire.h>
2+
#include "Encoder.h"
3+
#include "Battery.h"
4+
#include "DCMotor.h"
5+
#include "ServoMotor.h"
6+
#include "MotorController.h"
7+
#include "Common.h"
8+
#include "PID.h"

src/MotorController.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "MotorController.h"
2+
#include "Common.h"
3+
4+
String mc::MotorController::getFWVersion() {
5+
char ret[5];
6+
getData(GET_VERSION, (uint8_t*)ret);
7+
ret[4] = '\0';
8+
return String(ret);
9+
}
10+
11+
void mc::MotorController::reboot() {
12+
setData(RESET, 0, 0);
13+
}
14+
15+
void mc::MotorController::ping() {
16+
setData(PING, 0, 0);
17+
}
18+
19+
float mc::MotorController::getTemperature() {
20+
int ret;
21+
getData(GET_INTERNAL_TEMP, (uint8_t*)&ret);
22+
return (float)ret / 1000.0f;
23+
}

0 commit comments

Comments
 (0)