Loading...
Searching...
No Matches
dynamixel_reader.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017 Inria
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser General
5 * Public License v2.1. See the file LICENSE in the top level directory for more
6 * details.
7 */
8
20#ifndef DYNAMIXEL_READER_H
21#define DYNAMIXEL_READER_H
22
23#include <stdlib.h>
24#include <stdbool.h>
25
26#include "dynamixel_protocol.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#define DXL_PING_SIZE (10)
33#define DXL_STATUS_SIZE(len) (11+len)
34#define DXL_READ_SIZE (14)
35#define DXL_WRITE_SIZE(len) (12+len)
36
40typedef struct {
41 const uint8_t *buffer;
42 size_t size;
44
52static inline void dynamixel_reader_init(dynamixel_reader_t *reader, const uint8_t *buffer, size_t size)
53{
54 reader->buffer = buffer;
55 reader->size = size;
56}
57
67
75static inline uint8_t dynamixel_reader_get_id(const dynamixel_reader_t *reader)
76{
77 return reader->buffer[4];
78}
79
87static inline uint8_t dynamixel_reader_get_instr(const dynamixel_reader_t *reader)
88{
89 return reader->buffer[7];
90}
91
99static inline uint16_t dynamixel_reader_get_length(const dynamixel_reader_t *reader)
100{
101 return
102 (((uint16_t)reader->buffer[5]) & 0xFF) |
103 ((((uint16_t)reader->buffer[6]) & 0xFF) << 8);
104}
105
113static inline uint16_t dynamixel_reader_get_crc(const dynamixel_reader_t *reader)
114{
115 return
116 (((uint16_t)reader->buffer[reader->size - 2]) & 0xFF) |
117 ((((uint16_t)reader->buffer[reader->size - 1]) & 0xFF) << 8);
118}
119
127static inline const uint8_t *dynamixel_reader_status_get_payload(const dynamixel_reader_t *reader)
128{
129 return &reader->buffer[9];
130}
131
140{
141 return dynamixel_reader_get_length(reader) - 4;
142}
143
144#ifdef __cplusplus
145}
146#endif
147
148#endif /* DYNAMIXEL_READER_H */
Dynamixel protocol definitions.
static uint8_t dynamixel_reader_get_instr(const dynamixel_reader_t *reader)
Get the packet's instruction code.
static uint8_t dynamixel_reader_get_id(const dynamixel_reader_t *reader)
Get the packet's device id.
static size_t dynamixel_reader_status_get_payload_size(const dynamixel_reader_t *reader)
Get the packet's payload size (response)
static void dynamixel_reader_init(dynamixel_reader_t *reader, const uint8_t *buffer, size_t size)
Initialize the Dynamixel packet reader.
static const uint8_t * dynamixel_reader_status_get_payload(const dynamixel_reader_t *reader)
Get the packet's payload (response)
static uint16_t dynamixel_reader_get_length(const dynamixel_reader_t *reader)
Get the packet's length field.
bool dynamixel_reader_is_valid(const dynamixel_reader_t *reader)
Check if the packet is valid.
static uint16_t dynamixel_reader_get_crc(const dynamixel_reader_t *reader)
Get the packet's crc.
Dynamixel packet reader struct.
const uint8_t * buffer
data buffer
size_t size
data buffer's size