iCub-main
downloader.h
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4  * Copyright (C) 2008 RobotCub Consortium
5  * Author: Marco Maggiali, Marco Randazzo, Alessandro Scalzo, Marco Accame
6  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
7  *
8  */
9 
10 #ifndef DOWNLOADER_H
11 #define DOWNLOADER_H
12 
13 #include <yarp/os/Searchable.h>
14 #include <yarp/dev/CanBusInterface.h>
15 
16 #include <fstream>
17 #include "stdint.h"
18 
19 
20 //*****************************************************************/
21 
22 struct sBoard
23 {
24 public:
25  int bus; // the can bus
26  int pid; // the id inside the can bus
27  int type; // the board type. use macros ICUBCANPROTO_BOARDTYPE__DSP etc. or .. eObrd_type_t
28  bool applicationisrunning; // if true the board runs the application. if false it runs the bootloader
29  int appl_vers_major; // the major number of the version of the sw it is running (former ...)
30  int appl_vers_minor; // the minor number of the version of the sw it is running (former ...)
31  int appl_vers_build; // the build number of the version of the sw it is running (former ...). not meaningful for bootloader
32  int prot_vers_major; // the major number of the can protocol of the application. not meaningful for bootloader
33  int prot_vers_minor; // the minor number of the can protocol of the application. not meaningful for bootloader
34  char serial [50] = {""}; // only for strain
35  int strainregsetinuse; // only for strain
36  int strainregsetatboot; // only for strain
37  int status;
38  bool selected;
39  bool eeprom;
40  char add_info [50] = {""}; // but can protocol supports upt to 32 bytes
41 };
42 
43 //*****************************************************************/
44 
45 #define BOARD_RUNNING 0
46 #define BOARD_WAITING 1
47 #define BOARD_WAITING_ACK 2
48 #define BOARD_DOWNLOADING 3
49 #define BOARD_OK 4
50 #define BOARD_ERR 5
51 
52 #define SPRS_STATE_WAIT 0
53 #define SPRS_STATE_TYPE 1
54 #define SPRS_STATE_ADDRESS 2
55 #define SPRS_STATE_LENGTH 3
56 #define SPRS_STATE_DATA 4
57 #define SPRS_STATE_CHECKSUM 5
58 
59 #define SPRS_TYPE_0 '0'
60 #define SPRS_TYPE_1 '1'
61 #define SPRS_TYPE_3 '3'
62 #define SPRS_TYPE_4 '4'
63 #define SPRS_TYPE_7 '7'
64 
65 #define ID_MASTER 0x00
66 #define ID_BROADCAST 0x0F
67 
68 
69 //*****************************************************************/
70 
71 void drv_sleep (double time);
72 
73 //*****************************************************************/
74 
75 
76 
77 #include "driver.h"
78 
79 #include "EoBoards.h"
80 #include "EoCommon.h"
81 
82 #include <iCubCanProto_types.h>
83 
84 // it forces the use of the new driver2 interface
85 #define DOWNLOADER_USE_IDRIVER2
86 
87 
88 #if defined(DOWNLOADER_USE_IDRIVER2)
89 // if you define it, the canLoader shall be able to retrieve can boards from both CAN1 and CAN2
90 #define DOWNLOADER_ETH_SUPPORTS_MULTIBUS
91 #endif
92 
93 
95 {
96  uint8_t data[6];
97  strain2_ampl_regs_t() { data[0] = data[1] = data[2] = data[3] = data[4] = data[5] = 0; }
98  void load(void *mem, size_t s) { memmove(data, mem, sizeof(data)); }
99  void * memory() { return data; }
100  size_t size() { return 6; }
101 };
102 
103 //Luca
105 {
108 };
109 
110 
112 {
113 
114 private:
115 
116 #if defined(DOWNLOADER_USE_IDRIVER2)
117  iDriver2* m_idriver;
118  vector<CanPacket> txBuffer;
119  vector<CanPacket> rxBuffer;
120 #else
121  iDriver* m_idriver;
122  yarp::dev::CanBuffer txBuffer;
123  yarp::dev::CanBuffer rxBuffer;
124 #endif
125 
126  // the id of the can bus is moved inside the struct sBoard because we want to manage boards from different CAN buses.
127  int canbus_id;
128 
129  bool strain_is_acquiring_in_calibratedmode;
130 
131 private:
132 int download_motorola_line(char* line, int len, int bus, int board_pid);
133 int download_hexintel_line(char* line, int len, int bus, int board_pid, bool eeprom, int board_type);
134 
135 int build_id(int source, int dest);
136 int get_src_from_id (int id);
137 int get_dst_from_id (int id);
138 
139 int verify_ack(int command, int read_messages);
140 
141 //Luca
142 enum { ampl_gain_numberOf = 13 };
143 
144 
145 public:
146 
149 
154 unsigned int sprsPage;
155  std::fstream filestr;
157 
159 int initdriver(yarp::os::Searchable &config, bool verbose = true);
160 int stopdriver();
161 
162 int initschede ();
163 int startscheda (int bus, int board_pid, bool board_eeprom, int download_type);
164 int stopscheda (int bus, int board_pid);
165 int download_file (int bus, int board_pid, int download_type, bool eeprom);
166 int open_file (std::string file);
167 int change_card_address (int bus, int target_id, int new_id, int board_type);
168 int change_board_info (int bus, int target_id, char* board_info);
169 int get_board_info (int bus, int target_id, char* board_info);
170 int get_serial_no (int bus, int target_id, char* board_info);
171 int get_firmware_version(int bus, int target_id, eObrd_cantype_t boardtype, eObrd_info_t *info, bool &noreply);
172 int get_canbus_id ();
173 void set_canbus_id (int id);
174 
175 int strain_start_sampling (int bus, int target_id, string *errorstring = NULL);
176 int strain_stop_sampling (int bus, int target_id, string *errorstring = NULL);
177 
179 
180 // the strain2 has multiple (up to 3) regulation sets. with these functions we can get / set the regulation set in use inside the strain2.
181 // allowed values for regset are only strain_regset_one/two/three. with regsetmode we choose if the value is the one currently used or the one in eeprom.
182 // attention: funtion strain_set_regulationset() does not want strain_regset_inuse but only strain_regset_one/two/three
183 int strain_set_regulationset (int bus, int target_id, int regset = strain_regset_one, int regsetmode = strain_regsetmode_temporary, string *errorstring = NULL);
184 int strain_get_regulationset (int bus, int target_id, int &regset, const int regsetmode = strain_regsetmode_temporary, string *errorstring = NULL);
185 
186 
187 // the calibration of the offset is meaningful only for the calibration set in use
188 int strain_calibrate_offset (int bus, int target_id, icubCanProto_boardType_t boardtype, unsigned int middle_val, string *errorstring = NULL);
189 
190 int strain_calibrate_offset2 (int bus, int target_id, icubCanProto_boardType_t boardtype, const std::vector<strain2_ampl_discretegain_t> &gains, const std::vector<int16_t> &targets, string *errorstring = NULL);
191 
192 // they are not dependent on the regulation set or we cannot specify one
193 int strain_get_adc (int bus, int target_id, char channel, unsigned int& adc, int type, string *errorstring = NULL);
194 int strain_get_eeprom_saved (int bus, int target_id, bool* status, string *errorstring = NULL);
195 int strain_save_to_eeprom (int bus, int target_id, string *errorstring = NULL);
196 // funtions *curr_bias() apply only to strain_regset_inuse because the curr bias is not part of the regulation set. it is a volatile regulation.
197 int strain_get_curr_bias (int bus, int target_id, char channel, signed int& bias, string *errorstring = NULL);
198 int strain_set_curr_bias (int bus, int target_id, string *errorstring = NULL); // only for strain_regset_inuse because the curr bias is not saved in eeprom in the regulation set
199 int strain_set_curr_bias (int bus, int target_id, char channel, signed int bias, string *errorstring = NULL); // only for strain_regset_inuse because the curr bias is not saved in eeprom in the regulation set
200 int strain_reset_curr_bias (int bus, int target_id, string *errorstring = NULL); // only for strain_regset_inuse because the curr bias is not saved in eeprom in the regulation set
201 // serial number is unique and does not depend on regulation set
202 int strain_get_serial_number (int bus, int target_id, char* serial_number, string *errorstring = NULL);
203 int strain_set_serial_number (int bus, int target_id, const char* serial_number, string *errorstring = NULL);
204 
205 
206 // all of the following can be related to any calibration set: the one in use or one of set 1, 2, 3. default is the one in use (old strain have only one regulation set)
207 int strain_get_offset (int bus, int target_id, char channel, unsigned int& offset, int regset = strain_regset_inuse, string *errorstring = NULL);
208 int strain_set_offset (int bus, int target_id, char channel, unsigned int offset, int regset = strain_regset_inuse, string *errorstring = NULL);
209 
210 int strain_get_matrix_rc (int bus, int target_id, char r, char c, unsigned int& elem, int regset = strain_regset_inuse, string *errorstring = NULL);
211 int strain_set_matrix_rc (int bus, int target_id, char r, char c, unsigned int elem, int regset = strain_regset_inuse, string *errorstring = NULL);
212 
213 int strain_get_matrix_gain (int bus, int target_id, unsigned int& gain, int regset = strain_regset_inuse, string *errorstring = NULL);
214 int strain_set_matrix_gain (int bus, int target_id, unsigned int gain, int regset = strain_regset_inuse, string *errorstring = NULL);
215 
216 int strain_set_amplifier_regs(int bus, int target_id, unsigned char channel, const strain2_ampl_regs_t &ampregs, int regset = strain_regset_inuse, string *errorstring = NULL);
217 int strain_get_amplifier_regs(int bus, int target_id, unsigned char channel, strain2_ampl_regs_t &ampregs, int regset = strain_regset_inuse, string *errorstring = NULL);
218 
219 int strain_set_amplifier_discretegain(int bus, int target_id, unsigned char channel, strain2_ampl_discretegain_t ampset, int regset = strain_regset_inuse, string *errorstring = NULL);
220 
221 int strain_get_amplifier_gain_offset(int bus, int target_id, unsigned char channel, float &gain, uint16_t &offset, int regset = strain_regset_inuse, string *errorstring = NULL);
222 int strain_set_amplifier_gain_offset(int bus, int target_id, unsigned char channel, float gain, uint16_t offset, int regset = strain_regset_inuse, string *errorstring = NULL);
223 
224 int strain_get_calib_bias (int bus, int target_id, char channel, signed int& bias, int regset = strain_regset_inuse, string *errorstring = NULL);
225 int strain_set_calib_bias (int bus, int target_id, string *errorstring = NULL); // used only for strain_regset_inuse
226 int strain_set_calib_bias (int bus, int target_id, char channel, signed int bias, int regset = strain_regset_inuse, string *errorstring = NULL);
227 int strain_reset_calib_bias (int bus, int target_id, string *errorstring = NULL); // used only for strain_regset_inuse
228 
229 int strain_set_full_scale (int bus, int target_id, unsigned char channel, unsigned int full_scale, int regset = strain_regset_inuse, string *errorstring = NULL);
230 int strain_get_full_scale (int bus, int target_id, unsigned char channel, unsigned int& full_scale, int regset = strain_regset_inuse, string *errorstring = NULL);
231 
232 
233 
234 
235 
236 
237 // for use by the future strain calibration data acquisition gui
238 
240 {
241  bool valid; // the acquisition is meaninful
242  bool calibrated; // the values are calibrated
243  bool saturated; // at least one measure is saturated. see saturationinfo[6] to know more
244  unsigned int channel[6]; // the values
245  icubCanProto_strain_saturationInfo_t saturationinfo[6]; // the saturation info
246  strain_value_t() : calibrated(false), valid(false), saturated(false) {
247  channel[0] = channel[1] = channel[2] = channel[3] = channel[4] = channel[5] = 0;
248  saturationinfo[0] = saturationinfo[1] = saturationinfo[2] = saturationinfo[3] = saturationinfo[4] = saturationinfo[5] = saturationNONE;
249  }
250  void extract(signed short *ss6) const {
251  if(NULL == ss6) return;
252  for(size_t i=0; i<6; i++) { ss6[i] = static_cast<unsigned short>(channel[i]) - 0x7fff; }
253  }
254 };
255 
256 typedef enum
257 {
261 
262 int strain_acquire_start(int bus, int target_id, uint8_t txratemilli = 20, bool calibmode = true, strain_acquisition_mode_t acqmode = strain_acquisition_mode_streaming, string *errorstring = NULL);
263 int strain_acquire_get(int bus, int target_id, vector<strain_value_t> &values, const unsigned int howmany = 10, void (*updateProgressBar)(void*, float) = NULL, void *arg = NULL, strain_acquisition_mode_t acqmode = strain_acquisition_mode_streaming, const unsigned int maxerrors = 1, string *errorstring = NULL);
264 int strain_acquire_stop(int bus, int target_id, strain_acquisition_mode_t acqmode = strain_acquisition_mode_streaming, string *errorstring = NULL);
265 
266 cDownloader(bool verbose = true);
267 
268 void set_verbose(bool verbose);
269 
270 void set_external_logger(void *caller = NULL, void (*logger)(void *, const std::string &) = NULL);
271 
272 
273 private:
274  void clean_rx(void);
275 
276 #if defined(DOWNLOADER_USE_IDRIVER2)
277  void set_bus(CanPacket &pkt, int bus);
278  int get_bus(CanPacket &pkt);
279 #else
280  void set_bus(yarp::dev::CanMessage &msg, int bus);
281  int get_bus(yarp::dev::CanMessage &msg);
282 #endif
283 
284  bool _verbose;
285 
286 
287  int strain_calibrate_offset2_strain1(int bus, int target_id, int16_t t, string *errorstring);
288  int strain_calibrate_offset2_strain1safer(int bus, int target_id, int16_t t, uint8_t nmeasures, bool fullsearch, string *errorstring);
289  int strain_calibrate_offset2_strain2(int bus, int target_id, const std::vector<strain2_ampl_discretegain_t> &gains, const std::vector<int16_t> &targets, string *errorstring = NULL);
290 
291  int readADC(int bus, int target_id, int channel, int nmeasures = 2);
292 
293  void (*_externalLoggerFptr)(void *caller, const std::string &output);
294  void * _externalLoggerCaller;
295 
296  void Log(const std::string &msg);
297 
298 };
299 
300 #endif
301 
302 // eof
303 
int open_file(std::string file)
int strain_get_serial_number(int bus, int target_id, char *serial_number, string *errorstring=NULL)
Definition: downloader.cpp:672
int strain_set_matrix_gain(int bus, int target_id, unsigned int gain, int regset=strain_regset_inuse, string *errorstring=NULL)
Definition: downloader.cpp:891
int strain_acquire_start(int bus, int target_id, uint8_t txratemilli=20, bool calibmode=true, strain_acquisition_mode_t acqmode=strain_acquisition_mode_streaming, string *errorstring=NULL)
int strain_get_offset(int bus, int target_id, char channel, unsigned int &offset, int regset=strain_regset_inuse, string *errorstring=NULL)
Definition: downloader.cpp:399
int change_card_address(int bus, int target_id, int new_id, int board_type)
int strain_get_full_scale(int bus, int target_id, unsigned char channel, unsigned int &full_scale, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_get_calib_bias(int bus, int target_id, char channel, signed int &bias, int regset=strain_regset_inuse, string *errorstring=NULL)
Definition: downloader.cpp:444
int strain_reset_curr_bias(int bus, int target_id, string *errorstring=NULL)
Definition: downloader.cpp:622
int get_canbus_id()
int get_board_info(int bus, int target_id, char *board_info)
int strain_set_offset(int bus, int target_id, char channel, unsigned int offset, int regset=strain_regset_inuse, string *errorstring=NULL)
int startscheda(int bus, int board_pid, bool board_eeprom, int download_type)
int strain_set_serial_number(int bus, int target_id, const char *serial_number, string *errorstring=NULL)
Definition: downloader.cpp:644
int strain_acquire_stop(int bus, int target_id, strain_acquisition_mode_t acqmode=strain_acquisition_mode_streaming, string *errorstring=NULL)
int strain_set_amplifier_discretegain(int bus, int target_id, unsigned char channel, strain2_ampl_discretegain_t ampset, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_get_regulationset(int bus, int target_id, int &regset, const int regsetmode=strain_regsetmode_temporary, string *errorstring=NULL)
Definition: downloader.cpp:837
int stopscheda(int bus, int board_pid)
int strain_save_to_eeprom(int bus, int target_id, string *errorstring=NULL)
Definition: downloader.cpp:214
void set_verbose(bool verbose)
Definition: downloader.cpp:98
int strain_acquire_get(int bus, int target_id, vector< strain_value_t > &values, const unsigned int howmany=10, void(*updateProgressBar)(void *, float)=NULL, void *arg=NULL, strain_acquisition_mode_t acqmode=strain_acquisition_mode_streaming, const unsigned int maxerrors=1, string *errorstring=NULL)
int change_board_info(int bus, int target_id, char *board_info)
int file_length
Definition: downloader.h:153
@ strain_regsetmode_permanent
Definition: downloader.h:148
@ strain_regsetmode_temporary
Definition: downloader.h:148
int strain_get_adc(int bus, int target_id, char channel, unsigned int &adc, int type, string *errorstring=NULL)
Definition: downloader.cpp:348
int strain_stop_sampling(int bus, int target_id, string *errorstring=NULL)
cDownloader(bool verbose=true)
Definition: downloader.cpp:87
strain_acquisition_mode_t
Definition: downloader.h:257
@ strain_acquisition_mode_polling
Definition: downloader.h:259
@ strain_acquisition_mode_streaming
Definition: downloader.h:258
int initdriver(yarp::os::Searchable &config, bool verbose=true)
Definition: downloader.cpp:142
int strain_set_calib_bias(int bus, int target_id, char channel, signed int bias, int regset=strain_regset_inuse, string *errorstring=NULL)
void set_canbus_id(int id)
int strain_get_eeprom_saved(int bus, int target_id, bool *status, string *errorstring=NULL)
Definition: downloader.cpp:713
int get_firmware_version(int bus, int target_id, eObrd_cantype_t boardtype, eObrd_info_t *info, bool &noreply)
int get_serial_no(int bus, int target_id, char *board_info)
int strain_reset_calib_bias(int bus, int target_id, string *errorstring=NULL)
Definition: downloader.cpp:524
int strain_get_matrix_gain(int bus, int target_id, unsigned int &gain, int regset=strain_regset_inuse, string *errorstring=NULL)
Definition: downloader.cpp:746
int strain_get_matrix_rc(int bus, int target_id, char r, char c, unsigned int &elem, int regset=strain_regset_inuse, string *errorstring=NULL)
void set_external_logger(void *caller=NULL, void(*logger)(void *, const std::string &)=NULL)
Definition: downloader.cpp:103
int strain_set_full_scale(int bus, int target_id, unsigned char channel, unsigned int full_scale, int regset=strain_regset_inuse, string *errorstring=NULL)
sBoard * board_list
Definition: downloader.h:150
int stopdriver()
Definition: downloader.cpp:122
int strain_set_regulationset(int bus, int target_id, int regset=strain_regset_one, int regsetmode=strain_regsetmode_temporary, string *errorstring=NULL)
Definition: downloader.cpp:804
@ strain_regset_inuse
Definition: downloader.h:147
@ strain_regset_two
Definition: downloader.h:147
@ strain_regset_one
Definition: downloader.h:147
@ strain_regset_three
Definition: downloader.h:147
int strain_set_calib_bias(int bus, int target_id, string *errorstring=NULL)
Definition: downloader.cpp:476
int strain_get_amplifier_regs(int bus, int target_id, unsigned char channel, strain2_ampl_regs_t &ampregs, int regset=strain_regset_inuse, string *errorstring=NULL)
Definition: downloader.cpp:947
int strain_set_curr_bias(int bus, int target_id, string *errorstring=NULL)
Definition: downloader.cpp:575
std::fstream filestr
Definition: downloader.h:155
int strain_get_curr_bias(int bus, int target_id, char channel, signed int &bias, string *errorstring=NULL)
Definition: downloader.cpp:545
int strain_start_sampling(int bus, int target_id, string *errorstring=NULL)
int board_list_size
Definition: downloader.h:151
unsigned int sprsPage
Definition: downloader.h:154
int initschede()
int download_file(int bus, int board_pid, int download_type, bool eeprom)
bool connected
Definition: downloader.h:158
int strain_get_amplifier_gain_offset(int bus, int target_id, unsigned char channel, float &gain, uint16_t &offset, int regset=strain_regset_inuse, string *errorstring=NULL)
int nSelectedBoards
Definition: downloader.h:156
int strain_set_matrix_rc(int bus, int target_id, char r, char c, unsigned int elem, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_set_curr_bias(int bus, int target_id, char channel, signed int bias, string *errorstring=NULL)
int strain_set_amplifier_regs(int bus, int target_id, unsigned char channel, const strain2_ampl_regs_t &ampregs, int regset=strain_regset_inuse, string *errorstring=NULL)
Definition: downloader.cpp:913
int strain_set_amplifier_gain_offset(int bus, int target_id, unsigned char channel, float gain, uint16_t offset, int regset=strain_regset_inuse, string *errorstring=NULL)
float strain_amplifier_discretegain2float(strain2_ampl_discretegain_t c)
Definition: downloader.cpp:992
int strain_calibrate_offset2(int bus, int target_id, icubCanProto_boardType_t boardtype, const std::vector< strain2_ampl_discretegain_t > &gains, const std::vector< int16_t > &targets, string *errorstring=NULL)
int strain_calibrate_offset(int bus, int target_id, icubCanProto_boardType_t boardtype, unsigned int middle_val, string *errorstring=NULL)
void drv_sleep(double time)
Definition: downloader.cpp:26
strain2_ampl_discretegain_t
Definition: downloader.h:105
@ ampl_gain36
Definition: downloader.h:106
@ ampl_gain08
Definition: downloader.h:107
@ ampl_gain24
Definition: downloader.h:106
@ ampl_gain256
Definition: downloader.h:106
@ ampl_gain64
Definition: downloader.h:106
@ ampl_gain06
Definition: downloader.h:107
@ ampl_gain96
Definition: downloader.h:106
@ ampl_gain10
Definition: downloader.h:107
@ ampl_gain48
Definition: downloader.h:106
@ ampl_gain16
Definition: downloader.h:106
@ ampl_gain20
Definition: downloader.h:106
@ ampl_gain04
Definition: downloader.h:107
@ ampl_gain128
Definition: downloader.h:106
FILE * file
Definition: main.cpp:81
degrees offset
Definition: sine.m:4
degrees time
Definition: sine.m:5
icubCanProto_strain_saturationInfo_t saturationinfo[6]
Definition: downloader.h:245
void extract(signed short *ss6) const
Definition: downloader.h:250
unsigned int channel[6]
Definition: downloader.h:244
bool eeprom
Definition: downloader.h:39
char serial[50]
Definition: downloader.h:34
int appl_vers_major
Definition: downloader.h:29
int prot_vers_major
Definition: downloader.h:32
int prot_vers_minor
Definition: downloader.h:33
int strainregsetatboot
Definition: downloader.h:36
int appl_vers_minor
Definition: downloader.h:30
bool applicationisrunning
Definition: downloader.h:28
int bus
Definition: downloader.h:25
int type
Definition: downloader.h:27
int pid
Definition: downloader.h:26
char add_info[50]
Definition: downloader.h:40
int appl_vers_build
Definition: downloader.h:31
int status
Definition: downloader.h:37
int strainregsetinuse
Definition: downloader.h:35
bool selected
Definition: downloader.h:38
uint8_t data[6]
Definition: downloader.h:96
void load(void *mem, size_t s)
Definition: downloader.h:98