leptrino_force_torque.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, Imai Laboratory, Keio University.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * * Neither the name of the Imai Laboratory, nor the name of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  * Author: Mahisorn Wongphati
32  * Notice: Modified & copied from Leptrino CD example source code
33  */
34 
35 // =============================================================================
36 // CFS_Sample 本体部
37 //
38 // Filename: main.c
39 //
40 // =============================================================================
41 // Ver 1.0.0 2012/11/01
42 // =============================================================================
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 
48 #include <leptrino/pCommon.h>
49 #include <leptrino/rs_comm.h>
51 
52 #include <ros/ros.h>
53 #include <geometry_msgs/WrenchStamped.h>
54 
55 // =============================================================================
56 // マクロ定義
57 // =============================================================================
58 #define PRG_VER "Ver 1.0.0"
59 
60 // =============================================================================
61 // 構造体定義
62 // =============================================================================
63 typedef struct ST_SystemInfo
64 {
65  int com_ok;
66 } SystemInfo;
67 
68 // =============================================================================
69 // プロトタイプ宣言
70 // =============================================================================
71 void App_Init(void);
72 void App_Close(void);
73 ULONG SendData(UCHAR *pucInput, USHORT usSize);
74 void GetProductInfo(void);
75 void GetLimit(void);
76 void SerialStart(void);
77 void SerialStop(void);
78 
79 // =============================================================================
80 // モジュール変数定義
81 // =============================================================================
87 
88 std::string g_com_port;
89 int g_rate;
90 
91 #define TEST_TIME 0
92 
93 int main(int argc, char** argv)
94 {
95  ros::init(argc, argv, "leptrino");
96 
97  ros::NodeHandle nh;
98  ros::NodeHandle nh_private("~");
99  if (!nh_private.getParam("com_port", g_com_port))
100  {
101  ROS_WARN("Port is not defined, trying /dev/ttyUSB0");
102  g_com_port = "/dev/ttyUSB0";
103  }
104 
105  if (!nh_private.getParam("rate", g_rate))
106  {
107  ROS_WARN("Rate is not defined, using maximum 1.2 kHz");
108  g_rate = 1200;
109  }
110  ros::Rate rate(g_rate);
111 
112  std::string frame_id = "leptrino";
113  nh_private.getParam("frame_id", frame_id);
114 
115  int i, l = 0, rt = 0;
116  ST_RES_HEAD *stCmdHead;
117  ST_R_DATA_GET_F *stForce;
118  ST_R_GET_INF *stGetInfo;
119  ST_R_LEP_GET_LIMIT* stGetLimit;
120 
121  App_Init();
122 
123  if (gSys.com_ok == NG)
124  {
125  ROS_ERROR("%s open failed\n", g_com_port.c_str());
126  exit(0);
127  }
128 
129  // 製品情報取得
130  GetProductInfo();
131  while (ros::ok())
132  {
133  Comm_Rcv();
134  if (Comm_CheckRcv() != 0)
135  { //受信データ有
136  CommRcvBuff[0] = 0;
137 
139  if (rt > 0)
140  {
141  stGetInfo = (ST_R_GET_INF *)CommRcvBuff;
142  stGetInfo->scFVer[F_VER_SIZE] = 0;
143  ROS_INFO("Version: %s", stGetInfo->scFVer);
144  stGetInfo->scSerial[SERIAL_SIZE] = 0;
145  ROS_INFO("SerialNo: %s", stGetInfo->scSerial);
146  stGetInfo->scPName[P_NAME_SIZE] = 0;
147  ROS_INFO("Type: %s", stGetInfo->scPName);
148  break;
149  }
150  }
151  else
152  {
153  rate.sleep();
154  }
155  }
156 
157  GetLimit();
158  while (ros::ok())
159  {
160  Comm_Rcv();
161  if (Comm_CheckRcv() != 0)
162  { //受信データ有
163  CommRcvBuff[0] = 0;
164 
166  if (rt > 0)
167  {
168  stGetLimit = (ST_R_LEP_GET_LIMIT *)CommRcvBuff;
169  for (int i = 0; i < FN_Num; i++)
170  {
171  ROS_INFO("\tLimit[%d]: %f", i, stGetLimit->fLimit[i]);
172  conversion_factor[i] = stGetLimit->fLimit[i] * 1e-4;
173  }
174  break;
175  }
176  }
177  else
178  {
179  rate.sleep();
180  }
181  }
182 
183  ros::Publisher force_torque_pub = nh_private.advertise<geometry_msgs::WrenchStamped>("force_torque", 1);
184 
185  usleep(10000);
186 
187  // 連続送信開始
188  SerialStart();
189 
190 #if TEST_TIME
191  double dt_sum = 0;
192  int dt_count = 0;
193  ros::Time start_time;
194 #endif
195 
196  while (ros::ok())
197  {
198  Comm_Rcv();
199  if (Comm_CheckRcv() != 0)
200  { //受信データ有
201 
202 #if TEST_TIME
203  dt_count++;
204  dt_sum += (ros::Time::now() - start_time).toSec();
205  if (dt_sum >= 1.0)
206  {
207  ROS_INFO("Time test: read %d in %6.3f sec: %6.3f kHz", dt_count, dt_sum, (dt_count / dt_sum) * 0.001);
208  dt_count = 0;
209  dt_sum = 0.0;
210  }
211  start_time = ros::Time::now();
212 #endif
213 
214  memset(CommRcvBuff, 0, sizeof(CommRcvBuff));
216  if (rt > 0)
217  {
218  stForce = (ST_R_DATA_GET_F *)CommRcvBuff;
219  ROS_DEBUG_THROTTLE(0.1, "%d,%d,%d,%d,%d,%d", stForce->ssForce[0], stForce->ssForce[1], stForce->ssForce[2],
220  stForce->ssForce[3], stForce->ssForce[4], stForce->ssForce[5]);
221 
222  geometry_msgs::WrenchStampedPtr msg(new geometry_msgs::WrenchStamped);
223  msg->header.stamp = ros::Time::now();
224  msg->header.frame_id = frame_id;
225  msg->wrench.force.x = stForce->ssForce[0] * conversion_factor[0];
226  msg->wrench.force.y = stForce->ssForce[1] * conversion_factor[1];
227  msg->wrench.force.z = stForce->ssForce[2] * conversion_factor[2];
228  msg->wrench.torque.x = stForce->ssForce[3] * conversion_factor[3];
229  msg->wrench.torque.y = stForce->ssForce[4] * conversion_factor[4];
230  msg->wrench.torque.z = stForce->ssForce[5] * conversion_factor[5];
231  force_torque_pub.publish(msg);
232  }
233  }
234  else
235  {
236  rate.sleep();
237  }
238 
239  ros::spinOnce();
240  } //while
241 
242  SerialStop();
243  App_Close();
244  return 0;
245 }
246 
247 // ----------------------------------------------------------------------------------
248 // アプリケーション初期化
249 // ----------------------------------------------------------------------------------
250 // 引数 : non
251 // 戻り値 : non
252 // ----------------------------------------------------------------------------------
253 void App_Init(void)
254 {
255  int rt;
256 
257  //Commポート初期化
258  gSys.com_ok = NG;
259  rt = Comm_Open(g_com_port.c_str());
260  if (rt == OK)
261  {
262  Comm_Setup(460800, PAR_NON, BIT_LEN_8, 0, 0, CHR_ETX);
263  gSys.com_ok = OK;
264  }
265 
266 }
267 
268 // ----------------------------------------------------------------------------------
269 // アプリケーション終了処理
270 // ----------------------------------------------------------------------------------
271 // 引数 : non
272 // 戻り値 : non
273 // ----------------------------------------------------------------------------------
274 void App_Close(void)
275 {
276  printf("Application close\n");
277 
278  if (gSys.com_ok == OK)
279  {
280  Comm_Close();
281  }
282 }
283 
284 /*********************************************************************************
285  * Function Name : HST_SendResp
286  * Description : データを整形して送信する
287  * Input : pucInput 送信データ
288  * : 送信データサイズ
289  * Output :
290  * Return :
291  *********************************************************************************/
292 ULONG SendData(UCHAR *pucInput, USHORT usSize)
293 {
294  USHORT usCnt;
295  UCHAR ucWork;
296  UCHAR ucBCC = 0;
297  UCHAR *pucWrite = &CommSendBuff[0];
298  USHORT usRealSize;
299 
300  // データ整形
301  *pucWrite = CHR_DLE; // DLE
302  pucWrite++;
303  *pucWrite = CHR_STX; // STX
304  pucWrite++;
305  usRealSize = 2;
306 
307  for (usCnt = 0; usCnt < usSize; usCnt++)
308  {
309  ucWork = pucInput[usCnt];
310  if (ucWork == CHR_DLE)
311  { // データが0x10ならば0x10を付加
312  *pucWrite = CHR_DLE; // DLE付加
313  pucWrite++; // 書き込み先
314  usRealSize++; // 実サイズ
315  // BCCは計算しない!
316  }
317  *pucWrite = ucWork; // データ
318  ucBCC ^= ucWork; // BCC
319  pucWrite++; // 書き込み先
320  usRealSize++; // 実サイズ
321  }
322 
323  *pucWrite = CHR_DLE; // DLE
324  pucWrite++;
325  *pucWrite = CHR_ETX; // ETX
326  ucBCC ^= CHR_ETX; // BCC計算
327  pucWrite++;
328  *pucWrite = ucBCC; // BCC付加
329  usRealSize += 3;
330 
331  Comm_SendData(&CommSendBuff[0], usRealSize);
332 
333  return OK;
334 }
335 
336 void GetProductInfo(void)
337 {
338  USHORT len;
339 
340  ROS_INFO("Get sensor information");
341  len = 0x04; // データ長
342  SendBuff[0] = len; // レングス
343  SendBuff[1] = 0xFF; // センサNo.
344  SendBuff[2] = CMD_GET_INF; // コマンド種別
345  SendBuff[3] = 0; // 予備
346 
347  SendData(SendBuff, len);
348 }
349 
350 void GetLimit(void)
351 {
352  USHORT len;
353 
354  ROS_INFO("Get sensor limit");
355  len = 0x04;
356  SendBuff[0] = len; // レングス
357  SendBuff[1] = 0xFF; // センサNo.
358  SendBuff[2] = CMD_GET_LIMIT; // コマンド種別
359  SendBuff[3] = 0; // 予備
360 
361  SendData(SendBuff, len);
362 }
363 
364 void SerialStart(void)
365 {
366  USHORT len;
367 
368  ROS_INFO("Start sensor");
369  len = 0x04; // データ長
370  SendBuff[0] = len; // レングス
371  SendBuff[1] = 0xFF; // センサNo.
372  SendBuff[2] = CMD_DATA_START; // コマンド種別
373  SendBuff[3] = 0; // 予備
374 
375  SendData(SendBuff, len);
376 }
377 
378 void SerialStop(void)
379 {
380  USHORT len;
381 
382  printf("Stop sensor\n");
383  len = 0x04; // データ長
384  SendBuff[0] = len; // レングス
385  SendBuff[1] = 0xFF; // センサNo.
386  SendBuff[2] = CMD_DATA_STOP; // コマンド種別
387  SendBuff[3] = 0; // 予備
388 
389  SendData(SendBuff, len);
390 }
int Comm_CheckRcv(void)
Definition: rs_comm.cpp:233
#define PAR_NON
Definition: rs_comm.h:17
#define CMD_GET_LIMIT
void publish(const boost::shared_ptr< M > &message) const
Definition: pCommon.h:47
#define ROS_DEBUG_THROTTLE(rate,...)
int main(int argc, char **argv)
#define CMD_DATA_START
#define CHR_ETX
Definition: rs_comm.h:25
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
UCHAR SendBuff[512]
void GetLimit(void)
double conversion_factor[FN_Num]
void Comm_Close(void)
Definition: rs_comm.cpp:66
void App_Init(void)
#define F_VER_SIZE
Definition: pCommon.h:26
#define ROS_WARN(...)
SystemInfo gSys
unsigned char UCHAR
Definition: pCommon.h:11
#define SERIAL_SIZE
Definition: pCommon.h:24
#define P_NAME_SIZE
Definition: pCommon.h:25
UCHAR CommSendBuff[1024]
#define ROS_INFO(...)
SSHORT ssForce[FN_Num]
#define CMD_GET_INF
ROSCPP_DECL bool ok()
#define NG
Definition: pCommon.h:22
SCHAR scSerial[SERIAL_SIZE]
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void GetProductInfo(void)
std::string g_com_port
float fLimit[FN_Num]
bool sleep()
unsigned char ucBCC
Definition: rs_comm.cpp:245
#define CHR_DLE
Definition: rs_comm.h:31
ULONG SendData(UCHAR *pucInput, USHORT usSize)
int Comm_Open(const char *dev)
Definition: rs_comm.cpp:45
#define OK
Definition: pCommon.h:21
unsigned short USHORT
Definition: pCommon.h:13
int Comm_GetRcvData(UCHAR *buff)
Definition: rs_comm.cpp:210
SCHAR scFVer[F_VER_SIZE]
struct ST_SystemInfo SystemInfo
bool getParam(const std::string &key, std::string &s) const
#define CHR_STX
Definition: rs_comm.h:24
#define CMD_DATA_STOP
static Time now()
void SerialStart(void)
int Comm_SendData(UCHAR *buff, int l)
Definition: rs_comm.cpp:192
void App_Close(void)
SCHAR scPName[P_NAME_SIZE]
unsigned long ULONG
Definition: pCommon.h:15
#define BIT_LEN_8
Definition: rs_comm.h:22
void Comm_Rcv(void)
Definition: rs_comm.cpp:246
ROSCPP_DECL void spinOnce()
UCHAR CommRcvBuff[256]
#define ROS_ERROR(...)
void Comm_Setup(long baud, int parity, int bitlen, int rts, int dtr, char code)
Definition: rs_comm.cpp:87
void SerialStop(void)


leptrino_force_torque
Author(s):
autogenerated on Sun Oct 6 2019 03:45:38