How can I set ResetFcn?

5 ビュー (過去 30 日間)
기범
기범 2023 年 1 月 9 日
回答済み: TED MOSBY 2025 年 6 月 24 日
Hello,
I'm trying to set resetFcn.
My purpose is set initialcondition and target posture in every trainsets.
  1. s0 = [1 0 0 0] **s0 is initial state
  2. a0 = [1; 0; 0; 0;] ** a0 is initial action
  3. sG = [1 rand rand rand] **sG is target posture
Here is my code:
env.ResetFcn = @(in)motorResetFcn(in); // start to set ResetFcn called 'motorResetFcn'
Here is my code : motorResetFcn.m
function in = motorReset(in)
% randomize target posture
blk = sprintf('sphericalMotorRL/Target_Posture');
t = ['1' '2/3*pi*randn-pi/3' '2/3*pi*randn-pi/3' '2/3*pi*randn-pi/3'];
in = setBlockParameter(in,blk,'Value',num2str(t));
% randomize initial action and state
h = ['1' '0' '0' '0'];
blk = 'sphericalMotorRL/sphericalMotor/s';
in = setBlockParameter(in,blk,'InitialCondition',num2str(h));
h = ['1'; '0'; '0'; '0';];
blk = 'sphericalMotorRL/sphericalMotor/a';
in = setBlockParameter(in,blk,'InitialCondition',num2str(h));
end
My initialcondition and target posture is maxtrix,
I don't know how to declare maxtrix..... what do I have to fix?

回答 (1 件)

TED MOSBY
TED MOSBY 2025 年 6 月 24 日
Hi,
As you haven't mentioned the exact problem you are facing, I can address the issue with your code which I noticed:
You're trying to create matrix-valued strings. The variables t and h are being constructed as character arrays instead of proper numeric arrays that are converted to strings. You need to format the matrix properly using mat2str:
t = ['1' '2/3*pi*randn-pi/3' '2/3*pi*randn-pi/3' '2/3*pi*randn-pi/3'];
h = ['1' '0' '0' '0'];
h = ['1'; '0'; '0'; '0';];
These lines create character arrays (strings), not numerical arrays (matrices). For example, ['1' '0' '0' '0'] is the string "1000", not the row vector [1 0 0 0].
I have corrected the part of the code below:
function in = motorResetFcn(in)
blk = 'sphericalMotorRL/Target_Posture';
t = [1, 2/3*pi*randn - pi/3, 2/3*pi*randn - pi/3, 2/3*pi*randn - pi/3];
in = setBlockParameter(in, blk, 'Value', mat2str(t));
blk = 'sphericalMotorRL/sphericalMotor/s';
s0 = [1, 0, 0, 0];
in = setBlockParameter(in, blk, 'InitialCondition', mat2str(s0));
blk = 'sphericalMotorRL/sphericalMotor/a';
a0 = [1; 0; 0; 0];
in = setBlockParameter(in, blk, 'InitialCondition', mat2str(a0));
end
Here is more information on "mat2str" :
Hope this helps!

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by