MATLAB仿真Arduino Braccio机器人遇点索引不支持错误求助
解决MATLAB中Arduino Braccio机器人仿真的索引错误
问题描述
在MATLAB中仿真Arduino Braccio机器人取放任务时,运行代码触发以下错误:
Unable to perform assignment because dot indexing is not supported for variables of this type. Error in rigidbody (line 110) predictedConfig(i).JointPosition = angles(:, i);
出错的代码片段(Step 3部分):
% Get simulated model predictions angles = getModelPredictions(pickX, pickY, placeX, placeY); % Initialize predictedConfig as an array of JointState objects predictedConfig = repmat(robot.homeConfiguration, length(angles), 1); % Apply the predicted angles to the robot configuration for i = 1:length(angles) % Set joint position for each body predictedConfig(i).JointPosition = angles(:, i); end
错误原因
- 数据类型误解:
robot.homeConfiguration返回的是数值数组(对应各关节的角度值),并非JointState对象。用repmat复制后得到的仍是二维数值数组,无法使用.JointPosition这种点索引进行赋值。 - 维度逻辑错误:
getModelPredictions返回的是单行向量(6个元素),length(angles)结果为6,循环中angles(:,i)实际是取单个元素,不符合多帧关节配置的设计逻辑。
修复方案
方案1:单组关节角度直接展示
如果仅需展示一组预测角度的机器人姿态,无需循环,直接赋值即可:
%% Step 3: Integrate Model Predictions (Optional) % Main loop with simulated predictions pickX = 0.1; pickY = 0.0; placeX = 0.2; placeY = 0.1; % Get simulated model predictions angles = getModelPredictions(pickX, pickY, placeX, placeY); % 直接将预测角度作为机器人配置 predictedConfig = angles; % Display the robot with predicted angles show(robot, predictedConfig); title('Predicted Angles Pose'); drawnow; pause(2);
方案2:多帧关节配置动画展示
如果要模拟连续运动的多帧姿态,需修改getModelPredictions返回多组角度(每列对应一帧),并初始化二维数组存储配置:
%% Step 3: Integrate Model Predictions (Optional) % Main loop with simulated predictions pickX = 0.1; pickY = 0.0; placeX = 0.2; placeY = 0.1; % 获取多帧预测角度(每列对应一帧) angles = getModelPredictions(pickX, pickY, placeX, placeY); % 初始化二维数组,每行对应一帧的关节配置 predictedConfig = zeros(size(angles,2), size(robot.homeConfiguration,1)); % 为每帧赋值关节角度 for i = 1:size(angles,2) predictedConfig(i,:) = angles(:,i)'; end % 播放多帧姿态动画 figure; for i = 1:size(predictedConfig,1) show(robot, predictedConfig(i,:)); title(['Frame ', num2str(i), ' - Predicted Angles Pose']); drawnow; pause(0.5); end %% Function Definitions % 修改函数返回多帧角度(示例:3帧,每列对应一帧的6个关节角度) function angles = getModelPredictions(~, ~, ~, ~) angles = [0.5, 0.6, 0.7; 0.25, 0.3, 0.35; -0.5, -0.45, -0.4; 0.0, 0.05, 0.1; 0.5, 0.55, 0.6; 0.1, 0.15, 0.2]; end
关键说明
- 对于
robotics.RigidBodyTree对象,show函数直接接受数值数组作为关节配置参数,无需封装为JointState对象。 - 若需实现连续动画,需确保关节配置数组的每行对应一帧的角度值,循环调用
show并刷新画面即可。
内容的提问来源于stack exchange,提问作者aakarsh
相关产品推荐
相关产品推荐

