Controlling *Servo Motor*s with an *RaspberryPi* + *Servo/PWM-Pi Hat!* using *MATLAB*
6 views (last 30 days)
Show older comments
Hi, I have to control one or more Servo motors which are plugged in a Servo/PWM-Pi Hat which is plugged in a RaspBerryPi 3 on the other hand. VIA Matlab.
if true
% >> rpi = raspi
rpi =
raspi with properties:
DeviceAddress: 169.254.0.2
Port: 18732
BoardName: Raspberry Pi 3 Model B
AvailableLEDs: {'led0'}
AvailableDigitalPins: [4,5,6,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
AvailableSPIChannels: {'CE0','CE1'}
AvailableI2CBuses: {'i2c-1'}
AvailableWebcams: {}
I2CBusSpeed: 0
Supported peripherals
>> showPins(rpi) >> rpi.AvailableI2CBuses
ans =
cell
'i2c-1'
>> rpi.I2CBusSpeed
ans =
0
>> scanI2CBus(rpi,'i2c-1')
ans =
1×2 cell array
'0x40' '0x70'
>> i2csensor = i2cdev(rpi,'i2c-1','0x70')
i2csensor =
i2cdev with properties:
Bus: 'i2c-1'
Address: '0x70'
>> output1 = read(i2csensor,2)
output1 =
1×2 uint8 row vector
17 17
>> output2 = readRegister(i2csensor,14)
output2 =
uint8
0
>> i2csensor
%what do I type in here, to define the Servo motor which is connected to Pin 0 at the Servo/PWM Hat ?
>> s = servo(i2csensor,0) Undefined function 'servo' for input arguments of type 'raspi.internal.i2cdev'.
end
THANKS IN ADVANCE !!!!
2 Comments
Kenan Wood
on 15 Sep 2018
Edited: Kenan Wood
on 15 Sep 2018
Hi I had a bit of trouble with this too hope this helps.
With the data sheet for the PCA9685 https://www.nxp.com/docs/en/data-sheet/PCA9685.pdf and https://forums.adafruit.com/viewtopic.php?f=24&t=92012 is how I got mine to work.
mypi = raspi;
scanI2CBus(mypi,'i2c-1')
i2cpwm = i2cdev(mypi,'i2c-1','0x40')
servoMin = 150;
servoMax = 600;
% Set all 4 PWM to 0
write(i2cpwm,[hex2dec('FA') hex2dec('0')])
write(i2cpwm,[hex2dec('FB') hex2dec('0')])
% To control all 16 servos in one write
% write(i2cpwm,[hex2dec('FC') hex2dec('0')])
% write(i2cpwm,[hex2dec('FD') hex2dec('0')])
while true
prompt = 'Set servo value between 0, 180 degrees (99 to exit): ';
degrees = input(prompt)
if degrees == 99
return
end
prompt = 'Select servo between 1, 16: ';
servoNum = input(prompt);
myServoWrite(degrees, servoMin, servoMax, i2cpwm, servoNum)
end
function myServoWrite(degrees, servoMin, servoMax, i2cpwm, servoNum)
offCount = floor(degrees/180 * (servoMax - servoMin) + servoMin)
write(i2cpwm,[(hex2dec('08') + 4*servoNum) bitand(offCount,hex2dec('FF'))]); % Low byte
write(i2cpwm,[(hex2dec('09') + 4*servoNum) bitshift(offCount,-8)]); % High byte
end
Tobias Schuster
on 24 Nov 2021
Hi Kenan, your script worked for me as well on Matlab R2021b and Raspberry Pi 4 B.
However, using the operating system of the file "mathworks_raspbian_R21.2.0.zip" coming from the MATLAB Support Package for Raspberry Pi Hardware, I'm not able to control the servos. I can execute the script without any error message, but nothing happens on the hardware.
So far, I managed to control a DC Motor (direction and speed) which is also connected to the same Raspberry Pi via a motor hat. By using built-in functions like "configurePin(rpi,Pin,'PWM')" or "writePWMDutyCycle(rpi,Pin,speed)" I can access the relevant GPIO Pins. Maybe there is a posibility to realise a servo motor control in a similar way.
The support package also provides the functions
s = servo(mypi, 12, 'MinPulseDuration', 1e-3, 'MaxPulseDuration', 2e-3);
and
writePosition(s, 90);
but I don't know how to acces the servo in this way. Maybe it is only possible if the servo is directly connected to the Raspberry Pi without the motor hat in between.
Thank you in advance!
Answers (0)
Communities
More Answers in the Power Electronics Control
See Also
Categories
Find more on Run on Target Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!