|
| 1 | +# ArduinoMotorCarrier library |
| 2 | + |
| 3 | +## Methods |
| 4 | + |
| 5 | +### `BATTERY` |
| 6 | + |
| 7 | +Returns information about the battery connected to the carrier. |
| 8 | + |
| 9 | +#### Syntax |
| 10 | + |
| 11 | +``` |
| 12 | +battery.getRaw() |
| 13 | +battery.getConverted() |
| 14 | +battery.getFiltered() |
| 15 | +``` |
| 16 | + |
| 17 | +#### Returns |
| 18 | + |
| 19 | +* _getRaw()_: returns the raw ADC read from the battery as am integer. |
| 20 | +* _getConverted()_: returns the battery voltage converted to volts as a floating point. |
| 21 | +* _getFiltered()_: returns the battery voltage converted to volts and filtered in the last 10 seconds. |
| 22 | + |
| 23 | + |
| 24 | +#### Example |
| 25 | + |
| 26 | +``` |
| 27 | +
|
| 28 | +#include <ArduinoMotorCarrier.h> |
| 29 | +
|
| 30 | +//Variable to store the battery voltage |
| 31 | +float batteryVoltage; |
| 32 | +... |
| 33 | +void loop() { |
| 34 | + batteryVoltage = battery.getRaw()/236.0; //236 for Nano, 77 for MKR. |
| 35 | + Serial.print("Battery voltage: "); |
| 36 | + Serial.print(batteryVoltage,3); |
| 37 | + Serial.print("V, Raw "); |
| 38 | + Serial.println(battery.getRaw()); |
| 39 | + delay(5000); //wait for a few seconds |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +### `SERVO` |
| 44 | + |
| 45 | +Represent the servomotors available on the MotorCarrier. There are 4 headers for servomotors. |
| 46 | + |
| 47 | +#### Syntax |
| 48 | + |
| 49 | +``` |
| 50 | +servo1.setAngle(int) |
| 51 | +servo2.setAngle(int) |
| 52 | +servo3.setAngle(int) |
| 53 | +servo4.setAngle(int) |
| 54 | +``` |
| 55 | + |
| 56 | +#### Function |
| 57 | + |
| 58 | +* _setAngle(int)_: Set the rotation angle (from 0 to 180) |
| 59 | + |
| 60 | + |
| 61 | +### `MOTOR` |
| 62 | + |
| 63 | +There is 4 objects pointing to each possible motor: M1, M2, M3 and M4. |
| 64 | + |
| 65 | +#### Syntax |
| 66 | + |
| 67 | +```` |
| 68 | +M1.setDuty(int) |
| 69 | +M2.setDuty(int) |
| 70 | +M3.setDuty(int) |
| 71 | +M4.setDuty(int) |
| 72 | +```` |
| 73 | + |
| 74 | +#### Functions |
| 75 | + |
| 76 | +* _setDuty(int)_: Set the duty cycle of the dc motor (from -100 to +100) , 0 means stop. |
| 77 | + |
| 78 | + |
| 79 | +### `ENCODER` |
| 80 | + |
| 81 | +Represents the 2 quadrature encoders embedded in the carrier board. The two objects are encoder1 and encoder2. |
| 82 | + |
| 83 | +#### Syntax |
| 84 | + |
| 85 | +```` |
| 86 | +encoder1.getRawCount() |
| 87 | +encoder2.resetCounter(); |
| 88 | +```` |
| 89 | + |
| 90 | +#### Functions |
| 91 | + |
| 92 | +* _getRawCount()_: Returns the number of counts from start as an integer. |
| 93 | +* _resetCounter(int)_: Resets counter to a certain value. |
| 94 | + |
| 95 | + |
| 96 | +### `CONTROLLER` |
| 97 | + |
| 98 | +Represents the motor shield controller and exposes some high level functions. It also configures the battery charger (only on the NanoMotorCarrier) to start charging the battery. |
| 99 | + |
| 100 | +#### Syntax |
| 101 | + |
| 102 | +``` |
| 103 | +controller.getFWVersion() |
| 104 | +controller.reboot() |
| 105 | +``` |
| 106 | +### `PID` |
| 107 | + |
| 108 | +Allow setting Motor1 or Motor2 to a specific speed or position. There are two PID virtual objects in the controller: pid1 and pid2. pid1 acts on M1 and encoder1. pid2 acts on M2 and encoder2. It is advisable to control the motors using these functions. |
| 109 | + |
| 110 | +#### Syntax |
| 111 | + |
| 112 | +``` |
| 113 | +pid1. setGains(int P, int I, int D) |
| 114 | +``` |
| 115 | + |
| 116 | +#### Functions |
| 117 | + |
| 118 | +* _setGains(float P, float I, float D)_: Set PID gains. (`int` type for MKRMotorCarrier) |
| 119 | +* _resetGains()_: Reset PID gains to factory default settings. |
| 120 | +* _setControlMode(cl_control)_: Set control mode to either `CL_VELOCITY` or `CL_POSITION`. |
| 121 | +* _setSetpoint(cl_mode, int target)_: Set a specific velocity or position in one of the motors. Define cl_mode as `TARGET_POSITION` or `TARGET_VELOCITY` and the desired value in target. |
| 122 | + |
| 123 | +#### Example |
| 124 | + |
| 125 | +Example for PID position control. |
| 126 | + |
| 127 | +``` |
| 128 | +#include <ArduinoMotorCarrier.h> |
| 129 | +#define INTERRUPT_PIN 6 |
| 130 | +
|
| 131 | +//Variable to change the motor speed and direction |
| 132 | +static int duty = 0; |
| 133 | +
|
| 134 | +int target; |
| 135 | +float P; |
| 136 | +float I; |
| 137 | +float D; |
| 138 | +
|
| 139 | +void setup() |
| 140 | +{ |
| 141 | + //Serial port initialization |
| 142 | + Serial.begin(115200); |
| 143 | + while (!Serial); |
| 144 | +
|
| 145 | + //Establishing the communication with the motor shield |
| 146 | + if (controller.begin()) |
| 147 | + { |
| 148 | + Serial.print("MKR Motor Shield connected, firmware version "); |
| 149 | + Serial.println(controller.getFWVersion()); |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + Serial.println("Couldn't connect! Is the red led blinking? You may need to update the firmware with FWUpdater sketch"); |
| 154 | + while (1); |
| 155 | + } |
| 156 | +
|
| 157 | + // Reboot the motor controller; brings every value back to default |
| 158 | + Serial.println("reboot"); |
| 159 | + controller.reboot(); |
| 160 | + delay(500); |
| 161 | +
|
| 162 | + int dutyInit = 0; |
| 163 | + M1.setDuty(dutyInit); |
| 164 | + M2.setDuty(dutyInit); |
| 165 | + M3.setDuty(dutyInit); |
| 166 | + M4.setDuty(dutyInit); |
| 167 | + Serial.print("Duty: "); |
| 168 | + Serial.println(dutyInit); |
| 169 | +
|
| 170 | + P = 0.07f;//0.07 //0.2 |
| 171 | + I = 0.0f; |
| 172 | + D = 0.007f; |
| 173 | +
|
| 174 | + /************* PID 1 ***********************/ |
| 175 | +
|
| 176 | + pid1.setControlMode(CL_POSITION); |
| 177 | +
|
| 178 | + //pid1.resetGains(); |
| 179 | + pid1.setGains(P, I, D); //Proportional(change) Integral(change) Derivative |
| 180 | + Serial.print("P Gain: "); |
| 181 | + Serial.println((float)pid1.getPgain()); |
| 182 | + Serial.print("I Gain: "); |
| 183 | + Serial.println((float)pid1.getIgain()); |
| 184 | + Serial.print("D Gain: "); |
| 185 | + Serial.println((float)pid1.getDgain(), 7); |
| 186 | + Serial.println(""); |
| 187 | +
|
| 188 | + encoder1.resetCounter(0); |
| 189 | + Serial.print("encoder1: "); |
| 190 | + Serial.println(encoder1.getRawCount()); |
| 191 | + target = 5000; |
| 192 | + pid1.setSetpoint(TARGET_POSITION, target); |
| 193 | +} |
| 194 | +
|
| 195 | +void loop() { |
| 196 | +
|
| 197 | + Serial.print("encoder1: "); |
| 198 | + Serial.print(encoder1.getRawCount()); |
| 199 | + Serial.print(" target: "); |
| 200 | + Serial.println(target); |
| 201 | + if (encoder1.getRawCount() == target) { |
| 202 | + target += 1000; |
| 203 | + Serial.print("Target reached: Setting new target.."); |
| 204 | + pid1.setSetpoint(TARGET_POSITION, target); |
| 205 | + //delay(5000); |
| 206 | + } |
| 207 | +
|
| 208 | + delay(50); |
| 209 | +} |
| 210 | +``` |
0 commit comments