Send values from matlab app to raspberry pi to run servos using adafruit servo controller

4 次查看(过去 30 天)
Hello!
I have built a small robot using Servo motors and set up a raspberry pi with an Adafruit Servo Controller to control them for my HNC project. To program this I have written an App in Matlab which I intend to output 6 values to the robot for the position of each of the 6 servo motors. The servos are plugged into channels 0-5 on the servo control board.
The code I have on Pi to move these servos is as below:
from adafruit_servokit import ServoKit
from time import sleep
kit=ServoKit(channels=16)
for i in range (0,5):
kit.servo[i].set_pulse_width_range(500,2600) %(repeated for all 6)
end
a=0
while True:
b=a
a=int(input("enter angle 0-180:-"))
x=(a-b)/100
for i in range (0,5):
for j in range (0,100):
kit.servo[i].angle=b+(i*x)
end
end
end
This moves the 6 servos symultaneously to the same position, on a steady motion (I noticed it would move some and then the rest, which was not ideal.
I also have a matlab app which is 2600 lines of code, and it visualises the robot and allows the user to move it via the 6 degrees of freedom (x,y,z translation, pitch yaw roll) and calculates the required position for each servo as you do so. It then updates a plot3 which represents the robot.
I would like to find a way to link these, but I do not know how. my search-fu is failing me!
When the app runs a simulation (moving through pre-programmed points) it updates the plot3 every 0.1s, in small steps. I would like it to output a value for each servo motor at each of these steps to the raspberry pi, which is now linked to Matlab Online via the wifi. I can detect the pi on MAtlab, but don't know where to go next!
The Pi then needs to pick up these values and plug them through the correct code line (kit.servo[i].angle=<matlab output for servo(i)>) to move the servo.
Please can anyone help me to do this? How to I get them to talk to each other, and then run at the same time? Ideally, I would like the Pi to be constantly expecting inputs from MAtlab, and Matlab to send it 6 numbers (one for each servo) each time it updates the plot3.
Thankyou!

回答(1 个)

Chetan
Chetan 2023-8-25
Hi @Peter!
As per as my understanding you need to establish the connection between the raspberry pi and the MATLAB Online. Which can be done via the client server architecture over the same WIFI network.
To achieve this, you can refer to the following links:
Hope it helps!

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Raspberry Pi Hardware 的更多信息

产品


版本

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by