允許將 Motor1 或 Motor2 設置為特定的速度或位置。控製器中有兩個 PID 虛擬對象:pid1 和 pid2。 pid1 作用於 M1 和 encoder1。 pid2 作用於 M2 和 encoder2。建議使用這些函數控製電機。
用法
pid1. setGains(int P, int I, int D)
職能
setGains(float P, float I, float D)
:設置 PID 增益。 (MKRMotorCarrier 的int
類型)resetGains()
:將 PID 增益重置為出廠默認設置。setControlMode(cl_control)
:將控製模式設置為CL_VELOCITY
或CL_POSITION
。setSetpoint(cl_mode, int target)
:在其中一個電機中設置特定的速度或位置。將 cl_mode 定義為TARGET_POSITION
或TARGET_VELOCITY
以及目標中的所需值。
示例
PID 位置控製示例。
#include <ArduinoMotorCarrier.h>
#define INTERRUPT_PIN 6
//Variable to change the motor speed and direction
static int duty = 0;
int target;
float P;
float I;
float D;
void setup()
{
//Serial port initialization
Serial.begin(115200);
while (!Serial);
//Establishing the communication with the motor shield
if (controller.begin())
{
Serial.print("MKR Motor Shield connected, firmware version ");
Serial.println(controller.getFWVersion());
}
else
{
Serial.println("Couldn't connect! Is the red led blinking? You may need to update the firmware with FWUpdater sketch");
while (1);
}
// Reboot the motor controller; brings every value back to default
Serial.println("reboot");
controller.reboot();
delay(500);
int dutyInit = 0;
M1.setDuty(dutyInit);
M2.setDuty(dutyInit);
M3.setDuty(dutyInit);
M4.setDuty(dutyInit);
Serial.print("Duty: ");
Serial.println(dutyInit);
P = 0.07f;//0.07 //0.2
I = 0.0f;
D = 0.007f;
/************* PID 1 ***********************/
pid1.setControlMode(CL_POSITION);
//pid1.resetGains();
pid1.setGains(P, I, D); //Proportional(change) Integral(change) Derivative
Serial.print("P Gain: ");
Serial.println((float)pid1.getPgain());
Serial.print("I Gain: ");
Serial.println((float)pid1.getIgain());
Serial.print("D Gain: ");
Serial.println((float)pid1.getDgain(), 7);
Serial.println("");
encoder1.resetCounter(0);
Serial.print("encoder1: ");
Serial.println(encoder1.getRawCount());
target = 5000;
pid1.setSetpoint(TARGET_POSITION, target);
}
void loop() {
Serial.print("encoder1: ");
Serial.print(encoder1.getRawCount());
Serial.print(" target: ");
Serial.println(target);
if (encoder1.getRawCount() == target) {
target += 1000;
Serial.print("Target reached: Setting new target..");
pid1.setSetpoint(TARGET_POSITION, target);
//delay(5000);
}
delay(50);
}
相關用法
- Arduino ArduinoMotorCarrier - BATTERY用法及代碼示例
- Arduino Arduino_EMBRYO_2 - setLengthXY()用法及代碼示例
- Arduino ArduinoBLE - bleDevice.advertisedServiceUuidCount()用法及代碼示例
- Arduino ArduinoBLE - BLEService()用法及代碼示例
- Arduino ArduinoBLE - bleCharacteristic.subscribe()用法及代碼示例
- Arduino Arduino_LSM9DS1 - readGyroscope()用法及代碼示例
- Arduino ArduinoSound - FFTAnalyzer.input()用法及代碼示例
- Arduino Arduino_LSM9DS1 - magneticFieldAvailable()用法及代碼示例
- Arduino ArduinoBLE - BLE.poll()用法及代碼示例
- Arduino ArduinoBLE - bleCharacteristic.hasDescriptor()用法及代碼示例
- Arduino Arduino_APDS9960 - begin()用法及代碼示例
- Arduino ArduinoGraphics - text()用法及代碼示例
- Arduino Arduino_MKRGPS - standby()用法及代碼示例
- Arduino ArduinoBLE - BLEDescriptor()用法及代碼示例
- Arduino ArduinoGraphics - fill()用法及代碼示例
- Arduino Arduino_EMBRYO_2 - end()用法及代碼示例
- Arduino Arduino_LSM6DSOX - gyroscopeAvailable()用法及代碼示例
- Arduino Arduino_EMBRYO_2 - terminateInterrupt()用法及代碼示例
- Arduino ArduinoSound - AudioInI2S.sampleRate()用法及代碼示例
- Arduino Arduino_OplaUI - setSuffix()用法及代碼示例
- Arduino Arduino SigFox for MKRFox1200 - SigFox.begin()用法及代碼示例
- Arduino Arduino_MKRGPS - longitude()用法及代碼示例
- Arduino Arduino_MKRGPS - latitude()用法及代碼示例
- Arduino ArduinoBLE - bleCharacteristic.uuid()用法及代碼示例
- Arduino Arduino_MKRGPS - satellites()用法及代碼示例
注:本文由純淨天空篩選整理自arduino.cc大神的英文原創作品 ArduinoMotorCarrier - PID。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。