主要内容

Steer Two-Wheel Robot

This example shows how to use the MATLAB® Support Package for Arduino® Hardware to steer a two-wheel Arduino-based robot with closed-loop control.

Hardware Requirements

  • Arduino Due board

  • MinSegShield™ Kit M2V3.2

  • 9V battery pack

Hardware Setup

Assemble the hardware:

  • 将Minsegshield M2V3.2连接到Arduino育板。

  • Connect the Vcc of the battery pack to VIN port on the 2-terminal block on the MinSegShield.

  • Connect the Gnd of the battery pack to GND port on the 2-terminal block on the MinSegShield.

Create Rotary Encoder Objects

创建一个arduino对象,并包括'RotaryEncoder'图书馆。

a = arduino('com5','Due',“图书馆”,'RotaryEncoder')
Updating server code on board Due (COM5). This may take a few minutes.
a = arduino with properties: Port: 'COM5' Board: 'Due' AvailablePins: {'D2-D53', 'A0-A11'} AvailableDigitalPins: {'D2-D53', 'A0-A11'} AvailablePWMPins: {'D2-D13'} AvailableAnalogPins: {'A0-A11'} AvailableI2CBusIDs: [0, 1] Libraries: {'RotaryEncoder'}

Create rotary encoder objects for two encoders, specifying the connected output channel A and channel B pins. The quadrature encoder that comes with the kit has a resolution of 180 pulses per revolution.

Channela1 ='a8';channelB1 ='D15';Channela2 ='D18';channelb2 ='D19';encoder1= rotaryEncoder(a,channelA1,channelB1,180)
encoder1 = rotaryencoder with属性:channela:'a8'通道b:'d15'pulsesperrevolution:180
encoder2= rotaryEncoder(a,channelA2,channelB2,180)
encoder2= RotaryEncoder with properties: ChannelA: 'D18' ChannelB: 'D19' PulsesPerRevolution: 180

打开电动机

每个电动机都由PWM引脚控制速度,并且用于方向的数字引脚。要打开电动机,请将PWM电压设置为高于0,并将数字引脚输出设置为0或1,以前进或向后方向。

motor1SpeedPin ='d2';motor1DirectionPin ='D5';motor2SpeedPin ='D6';Motor2Directionpin ='D8';

Start rotating both motors by applying the same PWM voltage and setting the same direction.

direction = 0; initialPWMVoltage = 1.5; writeDigitalPin(a,motor1DirectionPin,direction); writeDigitalPin(a,motor2DirectionPin,direction); writePWMVoltage(a,motor1SpeedPin,initialPWMVoltage); writePWMVoltage(a,motor2SpeedPin,initialPWMVoltage);% wait for the change to take effect on the hardwarepause(3);

测量电动机速度

To measure one motor speed, pass in the corresponding encoder object.

rpm = readSpeed(encoder1); fprintf('Current motor speed is: %.2f\n',rpm);
当前电动机速度为:-29.17

To measure both motor speeds at the same time, pass in an array of the two encoder objects.

rpms = readSpeed([encoder1,encoder2]); fprintf('Current motor speeds are: %.2f, %.2f\n',rpms(1),rpms(2));
当前电动机速度为:-25.00,-33.33

Steer Robot Straight

通常,在每个车轮上施加相同的功率并不会导致两个电动机以相同的速度旋转,因此机器人不会笔直移动。为了使其直奔,使用闭环控制算法来根据其实际速度差来调整两个电动机的功率。在此示例中,比例控制器用于帮助调整速度。下图解释了控制器逻辑。

定义控制器执行时间,目标速度,采样周期和比例增益参数。

executionTime = 5;targetSpeed =-50; period = 0.1; Kp = 0.002;

Implement the closed-loop control algorithm to read both motor speeds and adjust one motor speed based on the difference. Make sure the shield is powered with 9V battery pack so that the motors rotate properly.

tic;尽管toc < executionTime rpms = readSpeed([encoder1,encoder2]); diff = rpms-targetSpeed; newPWMVoltages = initialPWMVoltage - diff*Kp; writePWMVoltage(a,motor1SpeedPin,newPWMVoltages(1)); writePWMVoltage(a,motor2SpeedPin,newPWMVoltages(2)); pause(period);end

Stop the motors by not applying power through the PWM pins.

writePWMVoltage(a,motor1SpeedPin,0); writePWMVoltage(a,motor2SpeedPin,0);

Note that the controller is running in soft real-time as it runs in MATLAB but not on the Arduino hardware. Hence, the quality of the control is affected by any other task running on your computer at the same time, such as anti-virus activities. To get hard real-time control, use theSimulink Arduino Support Package.

Clean Up

如果不再需要连接,请清除旋转编码器对象和arduino对象。

清除encoder1encoder2a