Why does Matlab Simscape send false values of the DC-Motor speed and position to Python (Through Real Time Data Transfer with TCP/IP)
4 views (last 30 days)
Show older comments
I created a DC-Motor model on Simscape with PID-Controller in ordner to control the speed of the DC-Motor. Then I created a Matlab Code, which I put in a "interpreted Matlab Function" Block on Matlab Simscape to send the motor data from the sensor directy and in Real Time to python.
The problem that I am encountering now, is that I get to see much higher values that get transferred to python that the real ones. I am suspecting that the MUX might be the problem since the Matlab interpreted function Block has one Input and I had to combine both parameters (Motor speed and position) with a Mux to send them with one input.
Here is a part of the model and also the configuration of the MUX on the MATLAB Simscape Model:


Here is the code that I put in the matlab function block:
function M = M(input)
% Function to calculate the load torque from Python based on speed and angle.
% Input:
% input - Vector of length 2: [omega, phi]
% Output:
% M - Load torque calculated by Python
% Persistent TCP client to maintain the connection across iterations
persistent t;
% Initialize the TCP client if it does not exist
if isempty(t)
t = tcpclient('127.0.0.1', 65432); % Connect to the Python server
end
% Extract speed (omega) and angle (phi) from the input vector
w = input(1);
phi = input(2);
% Debug output for input values
fprintf('Sending to Python: omega = %.2f, phi = %.2f\n', w, phi);
% Create JSON structure with current values and checksum
checksum = mod(w + phi, 256); % Simple checksum
data = struct('omega', w, 'phi', phi, 'checksum', checksum);
jsonData = jsonencode(data);
% Send the data to Python
write(t, uint8(jsonData));
% Receive response from Python (load torque)
pause(0.1); % Short pause for the response
if t.BytesAvailable > 0
response = read(t, t.BytesAvailable); % Read the response
responseData = jsondecode(char(response)); % Decode JSON data
% Debug output for received data
fprintf('Received from Python: %s\n', char(response));
% Validate the checksum
if isfield(responseData, 'checksum') && ...
responseData.checksum ~= mod(responseData.omega + responseData.phi, 256)
error('Checksum error: Data corruption detected!');
end
% Extract the load torque
M = responseData.lastmoment;
else
M = 0; % Default value if no response is received
warning('No response received from Python.');
end
end
Does the data that get sent need to be coded and decoded?
In Python I implemented a formula that calculates the load torque and sends the values back to Matlab and directly to the Motor and the scope..
I don't know what I am still doing wrong.. Any help would me much appreciated.
0 Comments
Answers (1)
Divyanshu
on 19 Dec 2024
I think the problem is not with MUX because it simply creates a virtual vector combining the given inputs. Instead the problem can be with how you are sending data to python.
The 'write' function used in the code of MATLAB Function block, is accepting a uint8(jsonData) as an argument.
But since the 'jsonData' holds a json encoded struct like {'omega':w,'phi':phi,'checksum',checksum}. So I think uint8(jsonData) might be encoding the data incorrectly.
See Also
Categories
Find more on Hardware-in-the-Loop Simulation with Simulink Real-Time 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!