gusimplewhiteboard
Giraff_MainSerialInterface.hpp
Go to the documentation of this file.
1
9#ifndef Giraff_MainSerialInterface_DEFINED
10#define Giraff_MainSerialInterface_DEFINED
11
13
14#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
15#pragma clang diagnostic push
16#pragma clang diagnostic ignored "-Wdeprecated"
17
18#include <arpa/inet.h>
19#include <sstream>
20
21#pragma clang diagnostic pop
22#endif
23
24namespace guWhiteboard
25{
47 {
48 public:
49
53
54#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
58 Giraff_MainSerialInterface(const std::string &str) { from_string(str); }
59
60#define DELIMITER ','
61#define SEP ':'
62#define PARSER(s, c, p) if (k.compare(s) == 0) { set_##c ( p ); continue; }
63#define SV std::vector<std::string>
64#define COMP(v, s) v.compare(0, 2, s) == 0
65#define IS_HEX(v) COMP(v, "F*") || COMP(v, "I*")
66
67#define READ_HEX(v) htonl(strtol(v.c_str()+2, NULL, 16))
68#define PARSE_HEX_FLOAT(v) static_cast<float>(READ_HEX(v))
69#define PARSE_HEX_INT32(v) static_cast<int32_t>(READ_HEX(v))
70#define PARSE_HEX_INT16(v) static_cast<int16_t>(READ_HEX(v))
71#define PARSE_HEX_INT8(v) static_cast<int8_t>(READ_HEX(v))
72
73#define PARSE_FLOAT IS_HEX(v) ? PARSE_HEX_FLOAT(v) : static_cast<float>(atof(v.c_str()))
74#define PARSE_INT16 IS_HEX(v) ? PARSE_HEX_INT16(v) : static_cast<int16_t>(atoi(v.c_str()))
75#define PARSE_INT32 IS_HEX(v) ? PARSE_HEX_INT32(v) : static_cast<int32_t>(atoi(v.c_str()))
76#define PARSE_INT8 IS_HEX(v) ? PARSE_HEX_INT8(v) : static_cast<uint8_t>(atoi(v.c_str()))
80 void from_string(const std::string &str)
81 {
82 SV sn = components_of_string_separated(str, DELIMITER, true);
83 for(size_t i = 0; i < sn.size(); i++)
84 {
85 SV kv = components_of_string_separated(sn.at(i), SEP, true);
86 if(kv.size() != 2)
87 {
88 fprintf(stderr, "parse error\n");
89 return;
90 }
91 std::string k = kv.at(0);
92 std::string v = kv.at(1);
93
94 //serial parser - removed indenting so I can read it, Carl.
95PARSER("v", velocity, PARSE_FLOAT)
96PARSER("r", movement_type, static_cast<Giraff_MovementType>(PARSE_FLOAT))
97PARSER("a", acceleration, PARSE_FLOAT)
98PARSER("p", position, PARSE_FLOAT)
99PARSER("mode", coord_type, static_cast<Giraff_CoordType>(PARSE_INT8))
100PARSER("tilt_angle_from_home", head_angle, PARSE_FLOAT)
101PARSER("vg", max_gear_ratio, PARSE_FLOAT)
102PARSER("vgr", gear_ratio_increments, PARSE_FLOAT)
103PARSER("cdp", gear_ratio_slowdown_dist, PARSE_FLOAT)
104PARSER("cvg", current_gear_ratio, PARSE_FLOAT)
105PARSER("but0", red_button_presses, PARSE_INT16)
106PARSER("but1", blue_button_presses, PARSE_INT16)
107PARSER("dial", dial_increments, PARSE_INT16)
108PARSER("enc0", left_motor_encoder_ticks, PARSE_INT32)
109PARSER("enc1", right_motor_encoder_ticks, PARSE_INT32)
110PARSER("enc2", head_motor_encoder_ticks, PARSE_INT32)
111
112 //pretty parser - removed indenting so I can read it, Carl.
113PARSER("velocity", velocity, PARSE_FLOAT)
114PARSER("movement_type", movement_type, static_cast<Giraff_MovementType>(PARSE_FLOAT))
115PARSER("acceleration", acceleration, PARSE_FLOAT)
116PARSER("position", position, PARSE_FLOAT)
117PARSER("coord_type", coord_type, static_cast<Giraff_CoordType>(PARSE_INT8))
118PARSER("head_angle", head_angle, PARSE_FLOAT)
119PARSER("max_gear_ratio", max_gear_ratio, PARSE_FLOAT)
120PARSER("gear_ratio_increments", gear_ratio_increments, PARSE_FLOAT)
121PARSER("gear_ratio_slowdown_dist", gear_ratio_slowdown_dist, PARSE_FLOAT)
122PARSER("current_gear_ratio", current_gear_ratio, PARSE_FLOAT)
123PARSER("red_button_presses", red_button_presses, PARSE_INT16)
124PARSER("blue_button_presses", blue_button_presses, PARSE_INT16)
125PARSER("dial_increments", dial_increments, PARSE_INT16)
126PARSER("left_motor_encoder_ticks", left_motor_encoder_ticks, PARSE_INT32)
127PARSER("right_motor_encoder_ticks", right_motor_encoder_ticks, PARSE_INT32)
128PARSER("head_motor_encoder_ticks", head_motor_encoder_ticks, PARSE_INT32)
129 }
130 }
131
135 std::string description() const
136 {
137 std::stringstream ss;
138 ss
139 << velocity() << "velocity : , "
140 << movement_type() << "movement_type : , "
141 << acceleration() << "acceleration : , "
142 << position() << "position : , "
143 << coord_type() << "coord_type : , "
144 << head_angle() << "head_angle : , "
145 << max_gear_ratio() << "max_gear_ratio : , "
146 << gear_ratio_increments() << "gear_ratio_increments : , "
147 << gear_ratio_slowdown_dist() << "gear_ratio_slowdown_dist : , "
148 << current_gear_ratio() << "current_gear_ratio : , "
149 << red_button_presses() << "red_button_presses : , "
150 << blue_button_presses() << "blue_button_presses : , "
151 << dial_increments() << "dial_increments : , "
152 << left_motor_encoder_ticks() << "left_motor_encoder_ticks : , "
153 << right_motor_encoder_ticks() << "right_motor_encoder_ticks : , "
154 << head_motor_encoder_ticks() << "head_motor_encoder_ticks : ";
155 return ss.str();
156 }
157#endif // WHITEBOARD_POSTER_STRING_CONVERSION
158 };
159}
160
161#endif //Giraff_MainSerialInterface_DEFINED
#define DELIMITER
#define PARSER(s, c, p)
#define PARSE_INT32
#define PARSE_INT16
#define PARSE_INT8
#define PARSE_FLOAT
Serial interface for the main giraff board.
Giraff_MainSerialInterface(const std::string &str)
string constructor (see from_string() below)
void from_string(const std::string &str)
parse class properties from a string
std::string description() const
pretty print method for showing the current property values
/file APM_Interface.h
Serial interface for the main giraff board.
Giraff_CoordType
Controls when position uses relative and absolute distances (absolute is based on odometry and is res...
Giraff_MovementType
/file wb_giraff_main_serial.h