Skip to content

Commit c69dc9c

Browse files
author
Kei
committed
updated sync read/write functions and example.
1 parent fb90dbf commit c69dc9c

File tree

4 files changed

+57
-39
lines changed

4 files changed

+57
-39
lines changed

examples/advanced/sync_read_write_raw/sync_read_write_raw.ino

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,54 +54,71 @@ typedef struct InfoSyncBulkInst{
5454
DYNAMIXEL::InfoSyncBulkInst_t sr_present_pos;
5555
const uint16_t user_pkt_buf_cap = 128;
5656
uint8_t user_pkt_buf[user_pkt_buf_cap];
57-
const uint16_t recv_buf_cap = 128;
58-
uint8_t recv_buf[recv_buf_cap];
59-
int32_t goal_vel = 0;
57+
58+
const uint8_t DXL_ID_CNT = 2;
59+
const uint16_t SR_START_ADDR = 126;
60+
const uint16_t SR_ADDR_LEN = 10; //2+4+4
61+
const uint16_t SW_START_ADDR = 104; //Goal velocity
62+
const uint16_t SW_ADDR_LEN = 4;
63+
typedef struct sr_data{
64+
int16_t present_current;
65+
int32_t present_velocity;
66+
int32_t present_position;
67+
} sr_data_t;
68+
uint8_t id_list[DXL_ID_CNT] = {1, 3};
69+
sr_data_t present_values[DXL_ID_CNT];
70+
int32_t goal_velocity[DXL_ID_CNT] = {100, 300};
6071

6172
Dynamixel2Arduino dxl(DXL_SERIAL, DXL_DIR_PIN);
6273

6374
void setup() {
6475
// put your setup code here, to run once:
76+
uint8_t i;
6577
DEBUG_SERIAL.begin(115200);
66-
dxl.begin(1000000);
67-
dxl.scan();
78+
dxl.begin(57600);
6879

69-
dxl.torqueOff(1);
70-
dxl.setOperatingMode(1, OP_VELOCITY);
71-
dxl.torqueOn(1);
72-
73-
dxl.torqueOff(3);
74-
dxl.setOperatingMode(3, OP_VELOCITY);
75-
dxl.torqueOn(3);
80+
for(i=0; i<DXL_ID_CNT; i++){
81+
dxl.torqueOff(id_list[i]);
82+
dxl.setOperatingMode(id_list[i], OP_VELOCITY);
83+
}
84+
for(i=0; i<DXL_ID_CNT; i++){
85+
dxl.torqueOn(id_list[i]);
86+
}
7687

77-
// Using user buffer
88+
// Generate SyncRead packet using user buffer
7889
sr_present_pos.p_packet_buf = user_pkt_buf;
7990
sr_present_pos.packet_buf_capacity = user_pkt_buf_cap;
91+
dxl.beginSyncRead(SR_START_ADDR, SR_ADDR_LEN, &sr_present_pos);
92+
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+
}
99+
}
80100

81-
dxl.beginSyncRead(132, 4, &sr_present_pos);
82-
dxl.addSyncReadID(1, &sr_present_pos);
83-
dxl.addSyncReadID(3, &sr_present_pos, true);
84-
85-
// Using class default buffer
86-
dxl.beginSyncWrite(104, 4);
87-
goal_vel = 100;
88-
dxl.addSyncWriteData(1, (uint8_t*)&goal_vel);
89-
goal_vel = 200;
90-
dxl.addSyncWriteData(3, (uint8_t*)&goal_vel);
101+
// Generate SyncRead packet using class default buffer
102+
dxl.beginSyncWrite(SW_START_ADDR, SW_ADDR_LEN);
103+
for(i=0; i<DXL_ID_CNT; i++){
104+
dxl.addSyncWriteData(id_list[i], (uint8_t*)&goal_velocity[i]);
105+
}
91106
dxl.sendSyncWrite();
92107
}
93108

94109
void loop() {
95110
// put your main code here, to run repeatedly:
96-
uint16_t recv_len;
111+
uint8_t recv_cnt;
97112

98-
recv_len = dxl.sendSyncRead(recv_buf, recv_buf_cap, &sr_present_pos);
99-
if(recv_len > 0){
100-
DEBUG_SERIAL.print("\t Receive Len : ");
101-
DEBUG_SERIAL.print(recv_len);
102-
DEBUG_SERIAL.println();
103-
for(uint16_t i=0; i<recv_len; i++){
104-
DEBUG_SERIAL.print(recv_buf[i], HEX);DEBUG_SERIAL.print(" ");
113+
recv_cnt = dxl.sendSyncRead((uint8_t*)&present_values, sizeof(present_values), &sr_present_pos);
114+
if(recv_cnt > 0){
115+
DEBUG_SERIAL.print("Received ID Count: ");
116+
DEBUG_SERIAL.println(recv_cnt);
117+
for(uint16_t i=0; i<recv_cnt; i++){
118+
DEBUG_SERIAL.print(" ID: ");DEBUG_SERIAL.println(id_list[i]);
119+
DEBUG_SERIAL.print("\t Present Current: ");DEBUG_SERIAL.println(present_values[i].present_current);
120+
DEBUG_SERIAL.print("\t Present Velocity: ");DEBUG_SERIAL.println(present_values[i].present_velocity);
121+
DEBUG_SERIAL.print("\t Present Position: ");DEBUG_SERIAL.println(present_values[i].present_position);
105122
}
106123
DEBUG_SERIAL.println();
107124
}else{

src/utility/master.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ Master::beginSyncRead(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_sy
551551
p_info->id_cnt = 0;
552552
p_info->packet_length = 0;
553553
p_info->is_complete_packet = false;
554+
p_info->addr_len = addr_len;
554555

555556
tx_param[param_len++] = addr >> 0;
556557
tx_param[param_len++] = addr >> 8;
@@ -615,10 +616,10 @@ Master::addSyncReadID(uint8_t id, InfoSyncBulkInst_t *p_sync_info, bool flag_end
615616
return ret;
616617
}
617618

618-
uint16_t
619+
uint8_t
619620
Master::sendSyncRead(uint8_t *p_recv_buf, uint16_t recv_buf_capacity, InfoSyncBulkInst_t *p_sync_info)
620621
{
621-
uint16_t total_recv_len = 0;
622+
uint8_t recv_cnt = 0;
622623
DXLLibErrorCode_t err = DXL_LIB_OK;
623624
InfoSyncBulkInst_t *p_info = &info_sync_bulk_;
624625

@@ -659,12 +660,13 @@ Master::sendSyncRead(uint8_t *p_recv_buf, uint16_t recv_buf_capacity, InfoSyncBu
659660

660661
for(uint8_t i=0; i<p_info->id_cnt; i++)
661662
{
662-
if(rxStatusPacket(&p_recv_buf[total_recv_len], recv_buf_capacity-total_recv_len) != nullptr){
663-
total_recv_len += info_rx_packet_.recv_param_len;
663+
if(rxStatusPacket(&p_recv_buf[i*p_info->addr_len],
664+
recv_buf_capacity-i*p_info->addr_len, 3) != nullptr){
665+
recv_cnt++;
664666
}
665667
}
666668

667-
return total_recv_len;
669+
return recv_cnt;
668670
}
669671

670672

src/utility/master.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Master
111111
/* Easy functions for Sync Read */
112112
bool beginSyncRead(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_sync_info = nullptr);
113113
bool addSyncReadID(uint8_t id, InfoSyncBulkInst_t *p_sync_info = nullptr, bool flag_end_add = false);
114-
uint16_t sendSyncRead(uint8_t *p_recv_buf, uint16_t recv_buf_capacity, InfoSyncBulkInst_t *p_sync_info = nullptr);
114+
uint8_t sendSyncRead(uint8_t *p_recv_buf, uint16_t recv_buf_capacity, InfoSyncBulkInst_t *p_sync_info = nullptr);
115115

116116
/* Easy functions for Sync Write */
117117
bool beginSyncWrite(uint16_t addr, uint16_t addr_len, InfoSyncBulkInst_t *p_sync_info = nullptr);

src/utility/slave.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,7 @@ Slave::rxInstPacket(uint8_t* p_param_buf, uint16_t param_buf_cap)
666666
begin_parse_dxl_packet(&info_rx_packet_, protocol_ver_idx_, p_param_buf, param_buf_cap);
667667
while(p_port_->available() > 0)
668668
{
669-
uint8_t c = p_port_->read();
670-
err = parse_dxl_packet(&info_rx_packet_, c);
669+
err = parse_dxl_packet(&info_rx_packet_, p_port_->read());
671670
if(err == DXL_LIB_OK){
672671
if((protocol_ver_idx_ == 2 && info_rx_packet_.inst_idx != DXL_INST_STATUS)
673672
|| protocol_ver_idx_ == 1){

0 commit comments

Comments
 (0)