定时发送力传感器数据至机器人

本文记录了一个实验过程,力传感器以100Hz频率通过定时器每10ms发送数据到机器人,机器人作为客户端连接到电脑服务端。实验中,下位机创建了读取socket和更新UI的同步任务,上位机使用了QT、Coin3D、多线程、QWT绘图等技术,实现了力传感器数据的接收和UI动态显示。
摘要由CSDN通过智能技术生成

每次完成一个demo都需要记下小笔记,防止忘记,实验如图:
在这里插入图片描述
在这里插入图片描述
第一张图中是红绿相接的部分是通讯结果,$表示接受完整,这里力传感器的频率为100hz,通过定时器每10ms向socket发送数据(只能有一个定时器运行!!不然发送时间不准,有滞后),机器人的同步运行周期设为了0.008s,最小控制周期是4ms,同步运行周期必须是其整数倍。
在这里插入图片描述
在这里插入图片描述
在下位机的start()函数里创建两个同步任务,一个用来读取socket,一个用来更新机器人示教器上的UI界面
本次实验以机器人为客户端,地址如图,电脑为服务端,监听端口号1000
在这里插入图片描述
下位机代码如下
从start()开始,(同步运行任务好像有行数限制,所以尽量多用子函数,否则会报错!!)

begin
  userPage()
  cls()
  gotoxy(0,0)
  put("Socket sensorRead测试程序:")
  taskCreateSync "readSocket",0.008,bSupervisor,readSocket(nError,nNum_FX,nNum_FY,nNum_FZ,nNum_TX,nNum_TY,nNum_TZ)
  taskCreateSync "UI",0.008,bSupervisor,UI(nNum_FX,nNum_FY,nNum_FZ,nNum_TX,nNum_TY,nNum_TZ)
  
end

readsocket()


begin
  // This program gets the parts sent by the other robot. 
  //  The parts arrive via a socket on a telegram with the format
  //  {ID=i,X=x.xxx,Y=y.yyy,RZ=r.rrr,L=l.llllll},
  //  where "{" is the initial character, "i" is the ID of the
  //  detected object, "x.xxx" is the value on X, "y.yyy" is the
  //  value on Y, "r.rrr" is the value on RZ, "l.llllll" is the
  //  latched encoder value, "," is the separator character and "}"
  //  is the end character. At the end, it sends the character "$" 
  //  as confirmation of good reception.
  // The format of the telegram is defined at the program 
  //  sendToNextRobot at the Motion library.
  //
  // Input Param
  //  none       : 
  //
  // Output Param
  //  x_nID         : ID of the detected object
  //  x_trObjectPos : Transformation of the detected object
  //  x_nEncLatchVal: Encoder latched value of the detected object
  //  x_nError      :
  //              0 = no error, one part detected.
  //             >1 = Number of parameters in error with in the
  //                   telegram read. It wasn't possible to fully
  //                   convert into a numerical value one of the
  //                   parameter within the telegram.
  //
  //{X=x.xxx,Y=y.yyy,Z=z.zzz,Rx=rx.xxx,Ry=ry.yyy,RZ=rz.zzz}
  //
  //----------------------------------------------------------------
  //
  //Init Variables
 x_nNum_FX = 0
 x_nNum_FY = 0
 x_nNum_FZ = 0
 x_nNum_TX = 0
  x_nNum_TY = 0
   x_nNum_TZ = 0
   
 
  x_nError=0
  //
  
  
  while true
     // Looks for int Character
  call waitCharOnSk("{")
  //
  // Looks for beginig of first value
  call waitCharOnSk("=")
  //
  // Recover X value
  call readValFromSk(",",x_nNum_FX,x_nError)
  if x_nError>0
    logMsg("(readSocket)>>"+sErrorCode[0])
  endIf
  //
  // Looks for beginig of next value
  call waitCharOnSk("=")
  //
  // Recover Y value
  call readValFromSk(",",x_nNum_FY,x_nError)
  if x_nError>0
    logMsg("(readSocket)>>"+sErrorCode[1])
  endIf
  //
  // Looks for beginig of next value
  call waitCharOnSk("=")
  //
  // Recover Z value
  call readValFromSk(",",x_nNum_FZ,x_nError)
  if x_nError>0
    logMsg("(readSocket)>>"+sErrorCode[2])
  endIf
  //
  // Looks for beginig of next value
  call waitCharOnSk("=")
  //
  //Recover Z value
  call readValFromSk(",",x_nNum_TX,x_nError)
  if x_nError>0
    logMsg("(readSocket)>>"+sErrorCode[3])
  endIf
  //
  // Looks for beginig of next value
  call waitCharOnSk("=")
  //
  //Recover Z value
  call readValFromSk(",",x_nNum_TY,x_nError)
  if x_nError>0
    logMsg("(readSocket)>>"+sErrorCode[4])
  endIf
  // 
  // Looks for beginig of next value
  call waitCharOnSk("=")
  //
  //Recover Z value
  call readValFromSk("}",x_nNum_TZ,x_nError)
  if x_nError>0
    logMsg("(readSocket)>>"+sErrorCode[5])
  endIf
  //
  //
  // Sends the character "$" as confirmation of good reception.
  sioSet(skFromPc,asc("$",0))
  
  delay(0)
  endWhile
 
  //
end

readvalFromSk()

begin
  // This program reads character by character on the socket until
  //  it finds a separator character. All the characters founded before
  //  this separator character are transformed on an integer value.
  //  The socket must be a socket server with timeout=0
  //
  // Input Param
  //  x_sCharacter : Character to find
  //
  // Output Param
  //  x_nValue     : Integer value read from the socket
  //  x_nError     :
  //             0 = No error
  //            +1 = Not possible to convert the string read into
  //                  a numerical value, or the string doesn't have
  //                  the right format.
  //
  //----------------------------------------------------------------
  //
  l_sStringRead=""
  l_sCharRead=""
  //
  // Read character by character until finds specific character.
  while l_sC
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值