@@ -16,6 +16,14 @@ static long convertDYNIXELXL330(int16_t degree) {
1616 return ret;
1717}
1818
19+ static long convertDYNIXELXL330_RT (int16_t degree) {
20+ M5_LOGI (" Degree: %d\n " , degree);
21+
22+ long ret = map (degree, -360 , 720 , -4095 , 8191 );
23+ M5_LOGI (" Position: %d\n " , ret);
24+ return ret;
25+ }
26+
1927// シリアルサーボ用のEasing関数
2028float quadraticEaseInOut (float p) {
2129 // return p;
@@ -34,6 +42,13 @@ StackchanSERVO::StackchanSERVO() {}
3442
3543StackchanSERVO::~StackchanSERVO () {}
3644
45+ float StackchanSERVO::getPosition (int x){
46+ if (_servo_type == RT_DYN_XL330){
47+ return _dxl.getPresentPosition (x);;
48+ } else {
49+ M5_LOGI (" getPosition::Command is only supprted in RT_DYN_XL330" );
50+ }
51+ };
3752
3853void StackchanSERVO::attachServos () {
3954 if (_servo_type == ServoType::SCS) {
@@ -68,6 +83,38 @@ void StackchanSERVO::attachServos() {
6883 // _dxl.torqueOff(AXIS_X + 1);
6984 // _dxl.torqueOff(AXIS_Y + 1);
7085
86+ } else if (_servo_type == ServoType::RT_DYN_XL330){
87+ M5_LOGI (" RT_DYN_XL330" );
88+ Serial2.begin (1000000 , SERIAL_8N1, _init_param.servo [AXIS_X].pin , _init_param.servo [AXIS_Y].pin );
89+ _dxl = Dynamixel2Arduino (Serial2);
90+ _dxl.begin (1000000 );
91+ _dxl.setPortProtocolVersion (DXL_PROTOCOL_VERSION);
92+ _dxl.ping (AXIS_X + 1 );
93+ _dxl.ping (AXIS_Y + 1 );
94+ _dxl.setOperatingMode (AXIS_X + 1 , OP_EXTENDED_POSITION);
95+ _dxl.setOperatingMode (AXIS_Y + 1 , OP_EXTENDED_POSITION);
96+ _dxl.writeControlTableItem (DRIVE_MODE, AXIS_X + 1 , 4 ); // Velocityのパラメータを移動時間(msec)で指定するモードに変更
97+ _dxl.writeControlTableItem (DRIVE_MODE, AXIS_Y + 1 , 4 ); // Velocityのパラメータを移動時間(msec)で指定するモードに変更
98+ _dxl.torqueOn (AXIS_X + 1 );
99+ delay (10 ); // ここでWaitを入れないと、Y(tilt)サーボが動かない場合がある。
100+ _dxl.torqueOn (AXIS_Y + 1 );
101+ delay (100 );
102+ _dxl.writeControlTableItem (PROFILE_VELOCITY, AXIS_X + 1 , 1000 );
103+ _dxl.writeControlTableItem (PROFILE_VELOCITY, AXIS_Y + 1 , 1000 );
104+ delay (100 );
105+
106+ if (_dxl.getPresentPosition (AXIS_X + 1 ) > 4096 ) {
107+ _init_param.servo [AXIS_X].offset = _init_param.servo [AXIS_X].offset + 360 ;
108+ }
109+ if (_dxl.getPresentPosition (AXIS_Y + 1 ) > 4096 ) {
110+ _init_param.servo [AXIS_Y].offset = _init_param.servo [AXIS_Y].offset + 360 ;
111+ }
112+
113+ _dxl.setGoalPosition (AXIS_X + 1 , convertDYNIXELXL330_RT (_init_param.servo [AXIS_X].start_degree + _init_param.servo [AXIS_X].offset ));
114+ _dxl.setGoalPosition (AXIS_Y + 1 , convertDYNIXELXL330_RT (_init_param.servo [AXIS_Y].start_degree + _init_param.servo [AXIS_Y].offset ));
115+ // _dxl.torqueOff(AXIS_X + 1);
116+ // _dxl.torqueOff(AXIS_Y + 1);
117+
71118 } else {
72119 // SG90 PWM
73120 if (_servo_x.attach (_init_param.servo [AXIS_X].pin ,
@@ -122,7 +169,16 @@ void StackchanSERVO::moveX(int x, uint32_t millis_for_move) {
122169 _isMoving = true ;
123170 vTaskDelay (millis_for_move/portTICK_PERIOD_MS);
124171 _isMoving = false ;
125- } else {
172+ } else if (_servo_type == ServoType::RT_DYN_XL330) {
173+ _dxl.writeControlTableItem (PROFILE_VELOCITY, AXIS_X + 1 , millis_for_move);
174+ vTaskDelay (10 /portTICK_PERIOD_MS);
175+ _dxl.setGoalPosition (AXIS_X + 1 , convertDYNIXELXL330_RT (x + _init_param.servo [AXIS_X].offset ));
176+ vTaskDelay (10 /portTICK_PERIOD_MS);
177+ _isMoving = true ;
178+ vTaskDelay (millis_for_move/portTICK_PERIOD_MS);
179+ _isMoving = false ;
180+ M5_LOGI (" X:%f" , getPosition (AXIS_X+1 ));
181+ }else {
126182 if (millis_for_move == 0 ) {
127183 _servo_x.easeTo (x + _init_param.servo [AXIS_X].offset );
128184 } else {
@@ -154,6 +210,15 @@ void StackchanSERVO::moveY(int y, uint32_t millis_for_move) {
154210 _isMoving = true ;
155211 vTaskDelay (millis_for_move/portTICK_PERIOD_MS);
156212 _isMoving = false ;
213+ } else if (_servo_type == ServoType::RT_DYN_XL330) {
214+ _dxl.writeControlTableItem (PROFILE_VELOCITY, AXIS_Y + 1 , millis_for_move);
215+ vTaskDelay (10 /portTICK_PERIOD_MS);
216+ _dxl.setGoalPosition (AXIS_Y + 1 , convertDYNIXELXL330_RT (y + _init_param.servo [AXIS_Y].offset )); // RT版に合わせて+180°しています。
217+ vTaskDelay (10 /portTICK_PERIOD_MS);
218+ _isMoving = true ;
219+ vTaskDelay (millis_for_move/portTICK_PERIOD_MS);
220+ _isMoving = false ;
221+ M5_LOGI (" Y:%f" , getPosition (AXIS_Y+1 ));
157222 } else {
158223 if (millis_for_move == 0 ) {
159224 _servo_y.easeTo (y + _init_param.servo [AXIS_Y].offset );
@@ -191,6 +256,12 @@ void StackchanSERVO::moveXY(int x, int y, uint32_t millis_for_move) {
191256 _dxl.setGoalPosition (AXIS_X + 1 , convertDYNIXELXL330 (x + _init_param.servo [AXIS_X].offset ));
192257 _dxl.setGoalPosition (AXIS_Y + 1 , convertDYNIXELXL330 (y + _init_param.servo [AXIS_Y].offset )); // RT版に合わせて+180°しています。
193258 _isMoving = false ;
259+ } else if (_servo_type == ServoType::RT_DYN_XL330) {
260+ _isMoving = true ;
261+ _dxl.setGoalPosition (AXIS_X + 1 , convertDYNIXELXL330_RT (x + _init_param.servo [AXIS_X].offset ));
262+ _dxl.setGoalPosition (AXIS_Y + 1 , convertDYNIXELXL330_RT (y + _init_param.servo [AXIS_Y].offset )); // RT版に合わせて+180°しています。
263+ _isMoving = false ;
264+ M5_LOGI (" X:%f, Y:%f" , getPosition (AXIS_X+1 ), getPosition (AXIS_Y+1 ));
194265 } else {
195266 _servo_x.setEaseToD (x + _init_param.servo [AXIS_X].offset , millis_for_move);
196267 _servo_y.setEaseToD (y + _init_param.servo [AXIS_Y].offset , millis_for_move);
@@ -215,6 +286,12 @@ void StackchanSERVO::moveXY(servo_param_s servo_param_x, servo_param_s servo_par
215286 _dxl.setGoalPosition (AXIS_X + 1 , convertDYNIXELXL330 (servo_param_x.degree + _init_param.servo [AXIS_X].offset ));
216287 _dxl.setGoalPosition (AXIS_Y + 1 , convertDYNIXELXL330 (servo_param_y.degree + _init_param.servo [AXIS_Y].offset )); // RT版に合わせて+180°しています。
217288 _isMoving = false ;
289+ } else if (_servo_type == ServoType::RT_DYN_XL330) {
290+ _isMoving = true ;
291+ _dxl.setGoalPosition (AXIS_X + 1 , convertDYNIXELXL330_RT (servo_param_x.degree + _init_param.servo [AXIS_X].offset ));
292+ _dxl.setGoalPosition (AXIS_Y + 1 , convertDYNIXELXL330_RT (servo_param_y.degree + _init_param.servo [AXIS_Y].offset )); // RT版に合わせて+180°しています。
293+ _isMoving = false ;
294+ M5_LOGI (" X:%f, Y:%f" , getPosition (AXIS_X+1 ), getPosition (AXIS_Y+1 ));
218295 } else {
219296 if (servo_param_x.degree != 0 ) {
220297 _servo_x.setEaseToD (servo_param_x.degree + servo_param_x.offset , servo_param_x.millis_for_move );
0 commit comments