z ¤ II- Webots Programming...

10
II- Webots Programming fundamentals

Transcript of z ¤ II- Webots Programming...

Page 1: z ¤ II- Webots Programming fundamentalssclab.yonsei.ac.kr/courses/12TCS/인지과학특강_webots_III.pdf · Supervisor programming • Supervisor is a special kind of Robot. –

z��¤ ��II- Webots Programming fundamentals

Page 2: z ¤ II- Webots Programming fundamentalssclab.yonsei.ac.kr/courses/12TCS/인지과학특강_webots_III.pdf · Supervisor programming • Supervisor is a special kind of Robot. –

S FT COMPUTING @ YONSEI UNIV . KOREA 16

Webots API

1

• Matlab APIx �p C�IhU W�"g�*Fh �&��� Cm =p tR¨��

• £�< C�&m� �6�, Wb_robot_init(), wb_robot_cleanup() �v ��­, ���&� ¡o£� d^��.

• MATLABhU� drawnow �>&8 a�p `_�u1 �,�8 �/� \ �^��.

Page 3: z ¤ II- Webots Programming fundamentalssclab.yonsei.ac.kr/courses/12TCS/인지과학특강_webots_III.pdf · Supervisor programming • Supervisor is a special kind of Robot. –

S FT COMPUTING @ YONSEI UNIV . KOREA 16

Controller programming -Basic

•  A prototype code of MATLAB controller TIME_STEP = 32; //The value 32 specifies the duration of the control steps while wb_robot_step(TIME_STEP) ~= -1 //This function synchronizes the controller’s data with the simulator. End //Note that in this example the while loop has no exit condition, hence the return statement is never reached.

2

Page 4: z ¤ II- Webots Programming fundamentalssclab.yonsei.ac.kr/courses/12TCS/인지과학특강_webots_III.pdf · Supervisor programming • Supervisor is a special kind of Robot. –

S FT COMPUTING @ YONSEI UNIV . KOREA 16

Controller programming – Reading sensors & Using actuators

TIME_STEP = 32; ds = wb_robot_get_device(‘ps(0)’);

1Nx )Hy] ¬� ps(0)v 0J �:ZU. 0J �A+x �y�8 ds1 ?¢

*ps(0)m �v y9'v device tag+ P:B, � 1Nx )Hy]K1 y9w �: �^��. wb_distance_sensor_enable(ds, TIME_STEP);

©� )Hy](ds)8 �:ZU1U Time_stepx _� � u1 ®X­.

while wb_robot_step(TIME_STEP) ~= -1 �M 5�F.

wb_distance_sensor_get_value(ds) �:��ZUx �w |ek� matlabhU� YG�2x iO1 ��:�iO8 ��¨��

wb_differential_wheels_set_speed(500, 500); �:ZUx �!iO8 �£�s¥ q�} (n� k7�x H� ] &)

end 3

Page 5: z ¤ II- Webots Programming fundamentalssclab.yonsei.ac.kr/courses/12TCS/인지과학특강_webots_III.pdf · Supervisor programming • Supervisor is a special kind of Robot. –

S FT COMPUTING @ YONSEI UNIV . KOREA 16

Controller programming – Reading sensors & Using actuators

TIME_STEP 32; servo = wb_robot_get_device("my_servo"); F = 2.0; // frequency 2 Hz t = 0.0; // elapsed simulation time while wb_robot_step(TIME_STEP) ~=-1 pos = sin(t * 2.0 * PI * F); wb_servo_set_position(servo, pos); t += TIME_STEP / 1000.0; End wb robot step() �y �S#*;� -> E�� {�-b �.�� ��~��. ¥Jh q�y� [ , ¯ (v .wbt x servo h D_"e�^��.

4

Page 6: z ¤ II- Webots Programming fundamentalssclab.yonsei.ac.kr/courses/12TCS/인지과학특강_webots_III.pdf · Supervisor programming • Supervisor is a special kind of Robot. –

S FT COMPUTING @ YONSEI UNIV . KOREA 16

Controller programming – Reading sensors & Using actuators together

#define TIME_STEP 32 //ZU Vf left_sensor = wb_robot_get_device("left_sensor"); right_sensor = wb_robot_get_device("right_sensor"); wb_distance_sensor_enable(left_sensor, TIME_STEP); wb_distance_sensor_enable(right_sensor, TIME_STEP); While wb_robot_step(TIME_STEP) ~= -1; // ZU |e'y� left_dist = wb_distance_sensor_get_value(left_sensor); double right_dist = wb_distance_sensor_get_value(right_sensor); // ª!�S ->compute_left_speed� `� �� §\� c��, left_distm right_dist� {�¥ jSw \ª©U [ 8 ��£��. � x xG1 L_C "�^��. left = compute_left_speed(left_dist, right_dist); right = compute_right_speed(left_dist, right_dist); // `� �! wb_differential_wheels_set_speed(left, right); end

5

Page 7: z ¤ II- Webots Programming fundamentalssclab.yonsei.ac.kr/courses/12TCS/인지과학특강_webots_III.pdf · Supervisor programming • Supervisor is a special kind of Robot. –

S FT COMPUTING @ YONSEI UNIV . KOREA 16

Controller programming – Controller termination

TIME_STEP = 64; for i=1:8 ps(i) = wb_robot_get_device(['ps' int2str(i-1)]); wb_distance_sensor_enable(ps(i),TIME_STEP); End while wb_robot_step(TIME_STEP) ~= -1 a = wb_distance_sensor_get_value(ps(1)) wb_differential_wheels_set_speed(500, 500); if a ==0 break; // whileFw Q���� �w Oi¦�p ��3.x �!y @�� `«y end // �4$��. ¡o¦�p saveExperimentsData() §\8 ��, `«w �: End //�T�1 �c% \ �^��.

6

Page 8: z ¤ II- Webots Programming fundamentalssclab.yonsei.ac.kr/courses/12TCS/인지과학특강_webots_III.pdf · Supervisor programming • Supervisor is a special kind of Robot. –

S FT COMPUTING @ YONSEI UNIV . KOREA 16

Supervisor programming

•  Supervisor is a special kind of Robot. – Like robot, It needs a controller and a controller also. – Supervisor node offers the wb supervisor *() functions.

• Robot vs. Supervisor distinction is intentional and aims at reminding the user that Supervisor code may not be easily transposed to real robots

7

Add ‘supervisor’ Node.

Link a controller file To ‘Controller’ Node. As like robot.

Page 9: z ¤ II- Webots Programming fundamentalssclab.yonsei.ac.kr/courses/12TCS/인지과학특강_webots_III.pdf · Supervisor programming • Supervisor is a special kind of Robot. –

S FT COMPUTING @ YONSEI UNIV . KOREA 16

Supervisor programming – Tracking the position

TIME_STEP = 64; robot_node = wb_supervisor_node_get_from_def('E');

�&h ��£� 1N(E)u1O� �y�8 |el��. trans_field = wb_supervisor_node_get_field(robot_node, 'translation');

1Nx �� �:h �� translation �y�8 |el��. while wb_robot_step(TIME_STEP) ~= -1 trans = wb_supervisor_field_get_sf_vec3f(trans_field);

trans_field hU 3�rx �L8 |el��. sprintf('MY_ROBOT is at position: %f %f %f \n', trans(1),trans(2),trans(3))

s��L �0 end

8

Page 10: z ¤ II- Webots Programming fundamentalssclab.yonsei.ac.kr/courses/12TCS/인지과학특강_webots_III.pdf · Supervisor programming • Supervisor is a special kind of Robot. –

S FT COMPUTING @ YONSEI UNIV . KOREA 16

Supervisor programming – Setting the position of robots

TIME_STEP = 64; % get handle to robot’s translation field robot_node = wb_supervisor_node_get_from_def('E'); trans_field = wb_supervisor_node_get_field(robot_node, 'translation'); for a = 0.0: +0.2 :5.0 for b = 0.0: +0.3 :10 % evaluate robot during 60 seconds (simulation time) for t=0.0: +TIME_STEP / 1000.0 : 60 wb_robot_step(TIME_STEP); end % compute travelled distance pos = wb_supervisor_field_get_sf_vec3f(trans_field); dist = sqrt(pos(1) * pos(1) + pos(3) * pos(3)); sprintf('a=%g, b=%g -> dist=%g\n', a, b, dist) % reset robot position INITIAL = [ 0, 0.5, 0 ]; wb_supervisor_field_set_sf_vec3f(trans_field, INITIAL); end end

9

1)60초동안 로봇동작 후, 2)로봇의 이동거리계산 3)wb_supervisor_field_set_sf_vec3f 를 통해 로봇의 새 위치 지정.