Skip to content

Commit 0cf0987

Browse files
author
Kei
committed
Modified & Added sync functions. (#23)
1 parent 5fef77b commit 0cf0987

File tree

3 files changed

+89
-38
lines changed

3 files changed

+89
-38
lines changed

examples/advanced/sync_read_write_raw/sync_read_write_raw.ino

+7-10
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ typedef struct sr_data{
6565
int32_t present_velocity;
6666
int32_t present_position;
6767
} sr_data_t;
68-
uint8_t id_list[DXL_ID_CNT] = {1, 3};
68+
uint8_t id_list[DXL_ID_CNT] = {1, 2};
6969
sr_data_t present_values[DXL_ID_CNT];
70-
int32_t goal_velocity[DXL_ID_CNT] = {100, 300};
70+
int32_t goal_velocity[DXL_ID_CNT] = {100, 200};
7171

7272
Dynamixel2Arduino dxl(DXL_SERIAL, DXL_DIR_PIN);
7373

@@ -88,21 +88,18 @@ void setup() {
8888
// Generate SyncRead packet using user buffer
8989
sr_present_pos.p_packet_buf = user_pkt_buf;
9090
sr_present_pos.packet_buf_capacity = user_pkt_buf_cap;
91-
dxl.beginSyncRead(SR_START_ADDR, SR_ADDR_LEN, &sr_present_pos);
91+
dxl.beginSetupSyncRead(SR_START_ADDR, SR_ADDR_LEN, &sr_present_pos);
9292
for(i=0; i<DXL_ID_CNT; i++){
93-
if(i<(DXL_ID_CNT-1)){
94-
dxl.addSyncReadID(id_list[i], &sr_present_pos);
95-
}else{
96-
//3rd parameter is a flag to finish adding parameter (ID) and create packet.(default is false)
97-
dxl.addSyncReadID(id_list[i], &sr_present_pos, true);
98-
}
93+
dxl.addSyncReadID(id_list[i], &sr_present_pos);
9994
}
95+
dxl.endSetupSyncRead(&sr_present_pos);
10096

10197
// Generate SyncRead packet using class default buffer
102-
dxl.beginSyncWrite(SW_START_ADDR, SW_ADDR_LEN);
98+
dxl.beginSetupSyncWrite(SW_START_ADDR, SW_ADDR_LEN);
10399
for(i=0; i<DXL_ID_CNT; i++){
104100
dxl.addSyncWriteData(id_list[i], (uint8_t*)&goal_velocity[i]);
105101
}
102+
dxl.endSetupSyncWrite();
106103
dxl.sendSyncWrite();
107104
}
108105

src/utility/master.cpp

+76-24
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ Master::clear(uint8_t id, uint8_t option, uint32_t ex_option, uint32_t timeout_m
531531
// (Protocol 1.0) Not Supported
532532
// (Protocol 2.0) Refer to http://emanual.robotis.com/docs/en/dxl/protocol2/#sync-read
533533
bool
534-
Master::beginSyncRead(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_sync_info)
534+
Master::beginSetupSyncRead(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_sync_info)
535535
{
536536
bool ret = false;
537537
DXLLibErrorCode_t err = DXL_LIB_OK;
@@ -574,7 +574,7 @@ Master::beginSyncRead(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_sy
574574
}
575575

576576
bool
577-
Master::addSyncReadID(uint8_t id, InfoSyncBulkInst_t *p_sync_info, bool flag_end_add)
577+
Master::addSyncReadID(uint8_t id, InfoSyncBulkInst_t *p_sync_info)
578578
{
579579
bool ret = false;
580580
DXLLibErrorCode_t err = DXL_LIB_OK;
@@ -599,23 +599,48 @@ Master::addSyncReadID(uint8_t id, InfoSyncBulkInst_t *p_sync_info, bool flag_end
599599

600600
if(err == DXL_LIB_OK){
601601
p_info->id_cnt++;
602-
if(flag_end_add == true){
603-
err = end_make_dxl_packet(&info_tx_packet_);
604-
if(err == DXL_LIB_OK){
605-
p_info->packet_length = info_tx_packet_.generated_packet_length;
606-
p_info->is_complete_packet = true;
607-
ret = true;
608-
}
609-
}else{
610-
ret = true;
611-
}
602+
ret = true;
612603
}
613604

614605
last_lib_err_ = err;
615606

616607
return ret;
617608
}
618609

610+
bool
611+
Master::endSetupSyncRead(InfoSyncBulkInst_t *p_sync_info)
612+
{
613+
bool ret = false;
614+
DXLLibErrorCode_t err = DXL_LIB_OK;
615+
InfoSyncBulkInst_t *p_info = &info_sync_bulk_;
616+
617+
// Parameter exception handling
618+
if(protocol_ver_idx_ != 2){
619+
err = DXL_LIB_ERROR_NOT_SUPPORTED;
620+
}else if(info_tx_packet_.inst_idx != DXL_INST_SYNC_READ){
621+
err = DXL_LIB_ERROR_NOT_INITIALIZED;
622+
}
623+
if(err != DXL_LIB_OK){
624+
last_lib_err_ = err;
625+
return false;
626+
}
627+
628+
if(p_sync_info != nullptr){
629+
p_info = p_sync_info;
630+
}
631+
632+
if(info_tx_packet_.inst_idx == DXL_INST_SYNC_READ){
633+
err = end_make_dxl_packet(&info_tx_packet_);
634+
if(err == DXL_LIB_OK){
635+
p_info->packet_length = info_tx_packet_.generated_packet_length;
636+
p_info->is_complete_packet = true;
637+
ret = true;
638+
}
639+
}
640+
641+
return ret;
642+
}
643+
619644
uint8_t
620645
Master::sendSyncRead(uint8_t *p_recv_buf, uint16_t recv_buf_capacity, InfoSyncBulkInst_t *p_sync_info)
621646
{
@@ -663,6 +688,8 @@ Master::sendSyncRead(uint8_t *p_recv_buf, uint16_t recv_buf_capacity, InfoSyncBu
663688
if(rxStatusPacket(&p_recv_buf[i*p_info->addr_len],
664689
recv_buf_capacity-i*p_info->addr_len, 3) != nullptr){
665690
recv_cnt++;
691+
}else{
692+
break;
666693
}
667694
}
668695

@@ -674,7 +701,7 @@ Master::sendSyncRead(uint8_t *p_recv_buf, uint16_t recv_buf_capacity, InfoSyncBu
674701
// (Protocol 1.0) Refer to http://emanual.robotis.com/docs/en/dxl/protocol1/#sync-write
675702
// (Protocol 2.0) Refer to http://emanual.robotis.com/docs/en/dxl/protocol2/#sync-write
676703
bool
677-
Master::beginSyncWrite(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_sync_info)
704+
Master::beginSetupSyncWrite(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_sync_info)
678705
{
679706
bool ret = false;
680707
DXLLibErrorCode_t err = DXL_LIB_OK;
@@ -717,7 +744,7 @@ Master::beginSyncWrite(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_s
717744
}
718745

719746
bool
720-
Master::addSyncWriteData(uint8_t id, uint8_t *p_data, InfoSyncBulkInst_t *p_sync_info, bool flag_end_add)
747+
Master::addSyncWriteData(uint8_t id, uint8_t *p_data, InfoSyncBulkInst_t *p_sync_info)
721748
{
722749
bool ret = false;
723750
DXLLibErrorCode_t err = DXL_LIB_OK;
@@ -743,16 +770,7 @@ Master::addSyncWriteData(uint8_t id, uint8_t *p_data, InfoSyncBulkInst_t *p_sync
743770
err = add_param_to_dxl_packet(&info_tx_packet_, (uint8_t*)p_data, p_info->addr_len);
744771
if(err == DXL_LIB_OK){
745772
p_info->id_cnt++;
746-
if(flag_end_add == true){
747-
err = end_make_dxl_packet(&info_tx_packet_);
748-
if(err == DXL_LIB_OK){
749-
p_info->packet_length = info_tx_packet_.generated_packet_length;
750-
p_info->is_complete_packet = true;
751-
ret = true;
752-
}
753-
}else{
754-
ret = true;
755-
}
773+
ret = true;
756774
}
757775
}
758776

@@ -761,6 +779,40 @@ Master::addSyncWriteData(uint8_t id, uint8_t *p_data, InfoSyncBulkInst_t *p_sync
761779
return ret;
762780
}
763781

782+
bool
783+
Master::endSetupSyncWrite(InfoSyncBulkInst_t *p_sync_info)
784+
{
785+
bool ret = false;
786+
DXLLibErrorCode_t err = DXL_LIB_OK;
787+
InfoSyncBulkInst_t *p_info = &info_sync_bulk_;
788+
789+
// Parameter exception handling
790+
if(protocol_ver_idx_ != 2){
791+
err = DXL_LIB_ERROR_NOT_SUPPORTED;
792+
}else if(info_tx_packet_.inst_idx != DXL_INST_SYNC_WRITE){
793+
err = DXL_LIB_ERROR_NOT_INITIALIZED;
794+
}
795+
if(err != DXL_LIB_OK){
796+
last_lib_err_ = err;
797+
return false;
798+
}
799+
800+
if(p_sync_info != nullptr){
801+
p_info = p_sync_info;
802+
}
803+
804+
if(info_tx_packet_.inst_idx == DXL_INST_SYNC_WRITE){
805+
err = end_make_dxl_packet(&info_tx_packet_);
806+
if(err == DXL_LIB_OK){
807+
p_info->packet_length = info_tx_packet_.generated_packet_length;
808+
p_info->is_complete_packet = true;
809+
ret = true;
810+
}
811+
}
812+
813+
return ret;
814+
}
815+
764816
bool
765817
Master::sendSyncWrite(InfoSyncBulkInst_t *p_sync_info)
766818
{

src/utility/master.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,15 @@ class Master
109109
// bool bulkWrite(uint8_t *p_param, uint16_t param_len, uint32_t timeout_ms = 10);
110110

111111
/* Easy functions for Sync Read */
112-
bool beginSyncRead(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_sync_info = nullptr);
113-
bool addSyncReadID(uint8_t id, InfoSyncBulkInst_t *p_sync_info = nullptr, bool flag_end_add = false);
112+
bool beginSetupSyncRead(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_sync_info = nullptr);
113+
bool addSyncReadID(uint8_t id, InfoSyncBulkInst_t *p_sync_info = nullptr);
114+
bool endSetupSyncRead(InfoSyncBulkInst_t *p_sync_info = nullptr);
114115
uint8_t sendSyncRead(uint8_t *p_recv_buf, uint16_t recv_buf_capacity, InfoSyncBulkInst_t *p_sync_info = nullptr);
115116

116117
/* Easy functions for Sync Write */
117-
bool beginSyncWrite(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_sync_info = nullptr);
118-
bool addSyncWriteData(uint8_t id, uint8_t *p_data, InfoSyncBulkInst_t *p_sync_info = nullptr, bool flag_end_add = false);
118+
bool beginSetupSyncWrite(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_sync_info = nullptr);
119+
bool addSyncWriteData(uint8_t id, uint8_t *p_data, InfoSyncBulkInst_t *p_sync_info = nullptr);
120+
bool endSetupSyncWrite(InfoSyncBulkInst_t *p_sync_info = nullptr);
119121
bool sendSyncWrite(InfoSyncBulkInst_t *p_sync_info = nullptr);
120122

121123
/* Easy functions for Bulk Read */

0 commit comments

Comments
 (0)