|
| 1 | +#include "MKRMotorShield.h" |
| 2 | + |
| 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 | + |
| 36 | +void setup() { |
| 37 | + |
| 38 | + Serial.begin(115200); |
| 39 | + while (!Serial); |
| 40 | + // create mkrshield connection |
| 41 | + Wire.begin(); |
| 42 | + |
| 43 | + delay(500); |
| 44 | + |
| 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); |
| 52 | + |
| 53 | + // enable dc motor2 object |
| 54 | + |
| 55 | + M2.setDuty(90); |
| 56 | + |
| 57 | + while(m2_pos < 36000){ |
| 58 | + m2_pos = encoder2.getRawCount(); |
| 59 | + controller.ping(); |
| 60 | + } |
| 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); |
| 69 | +} |
| 70 | +void loop() { |
| 71 | + m2_pos = encoder2.getRawCount(); |
| 72 | + Serial.println(m2_pos); |
| 73 | + controller.ping(); |
| 74 | +} |
0 commit comments