lc_client.py 14.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/python

import labcomm2014
import commands
import sys,traceback
import collections
import threading
import socket
import rwsocket

COMMAND_RECEIVED = 0
RESP_ACK         = 1
RESP_READ_PTP    = 2
RESP_MOVE_MEASURE= 3

GBS_ERROR = -1
17
GBS_OK = 0
18 19 20 21 22 23 24 25 26 27 28
WHAT = 0  #placeholder to be checked/changed

class ResponseMismatch(Exception):
    def __init__(self, expected, received):
        self.expected = expected
        self.received = received
    def __str__(self):
        return "Unexpected response: expected %s, got %s" % (self.expected, self.received)

class test_client(object):

29 30 31 32
    def __init__(self, host=socket.gethostname(), port=9876,version="LabComm2014.v1"):
        self.host=host
        self.port=port
        self.version=version
33 34 35 36 37

        #conditions for awaiting GBS responses
        self.response = threading.Condition()
        self.responses = collections.deque()

38 39 40 41 42 43 44 45
    def connect(self):
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server.connect((self.host, self.port))
        receiver_thread = threading.Thread(target=self.receiver, args=(client,))
        receiver_thread.start()

        self.d = labcomm2014.Decoder(labcomm2014.StreamReader(server), self.version)
        self.e = labcomm2014.Encoder(labcomm2014.StreamWriter(server), self.version)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

        self.e.add_decl(commands.CMD_MOVE_LIN.signature)
        self.e.add_decl(commands.CMD_MOVE_PTP.signature)
        self.e.add_decl(commands.CMD_NO_OPERATION.signature)
        self.e.add_decl(commands.CMD_READ_LIB.signature)
        self.e.add_decl(commands.CMD_READ_PTP.signature)
        self.e.add_decl(commands.CMD_LCK_TL.signature)
        self.e.add_decl(commands.CMD_UNLCK_TL.signature)
        self.e.add_decl(commands.CMD_NAIL_LIN.signature)
        self.e.add_decl(commands.CMD_MOVE_MEASURE.signature)
        self.e.add_decl(commands.CMD_MOVE_ARC.signature)
        self.e.add_decl(commands.CMD_MOVE.signature)

        self.d.register_handler(commands.COMMAND_RECEIVED.signature, self.handle_COMMAND_RECEIVED)
        self.d.register_handler(commands.RESP_ACK.signature, self.handle_RESP_ACK)
        self.d.register_handler(commands.RESP_READ_PTP.signature, self.handle_RESP_READ_PTP)
        self.d.register_handler(commands.RESP_MOVE_MEASURE.signature, self.handle_RESP_MOVE_MEASURE)

        self.e.add_decl(commands.shutdown.signature)
        self.d.register_handler(commands.shutdown.signature, self.handle_shutdown)

        self.cmd_seq=0
        self.run=True

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    def close(self):
        self.shutdown()

    def add_response(self, type, val):
        self.response.acquire()
        self.responses.append((type,val))
        self.response.notifyAll()
        self.response.release()

    def wait_response(self, type):
        self.response.acquire()
        while len(self.responses) < 1:
            self.response.wait()
        (t,res) = self.responses.popleft()
        self.response.release()
        if t == type:
            return res
        else:
            raise ResponseMismatch(type,t)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

    def handle_COMMAND_RECEIVED(self, val):
        print "got COMMAND_RECEIVED: %s" % val
        self.add_response(COMMAND_RECEIVED, val)

    def handle_RESP_NAK(self, val):
        print "got RESP_NAK: %s" % val
        self.add_response(RESP_NAK, val)

    def handle_RESP_ACK(self, val):
        print "got RESP_ACK: %s" % val
        self.add_response(RESP_ACK, val)

    def handle_RESP_READ_PTP(self, val):
        print "got RESP_READ_PTP: %s" % val
        self.add_response(RESP_READ_PTP, val)

    def handle_RESP_MOVE_MEASURE(self, val):
        print "got RESP_MOVE_MEASURE: %s" % val
        self.add_response(RESP_MOVE_MEASURE, val)

    def handle_shutdown(self, val):
        print "got shutdown: %s" % val
        self.run=False

    def send_command(self, cmd, sig):
        self.e.encode(cmd, sig)
        self.cmd_seq+=1

    def shutdown(self):
        self.send_command(self.cmd_seq, commands.shutdown.signature)


    def CMD_MOVE_LIN(self, coordinate, angle, speed, acc):
        self.send_command(dict(seq=self.cmd_seq,
                               cmd=dict(pos=dict(
                                   pos=coordinate,
                                   rot=angle),
                                S_Ac=dict(speed=speed, acc=acc))),
                          commands.CMD_MOVE_LIN.signature)
        res = self.wait_response(COMMAND_RECEIVED)
        print("... COMMAND_RECEIVED: %s"%res)
        res = self.wait_response(RESP_ACK)
        print("... RESP_ACK: %s"%res)
        res = self.wait_response(RESP_ACK)
        print("... RESP_ACK: %s"%res)
135
        return GBS_OK
136 137 138 139 140 141 142 143 144 145 146 147

    def CMD_MOVE_PTP(self, j, s, a):
        self.send_command(dict(seq=self.cmd_seq,
                               cmd=dict(j=j,
                                        S_Ac=dict(speed=s, acc=a))),
                          commands.CMD_MOVE_PTP.signature)
        res = self.wait_response(COMMAND_RECEIVED)
        print("... COMMAND_RECEIVED: %s"%res)
        res = self.wait_response(RESP_ACK)
        print("... RESP_ACK: %s"%res)
        res = self.wait_response(RESP_ACK)
        print("... RESP_ACK: %s"%res)
148
        return GBS_OK
149 150 151

    def CMD_NO_OPERATION(self):
        self.send_command(self.cmd_seq, commands.CMD_NO_OPERATION.signature)
152
        return GBS_OK
153 154
    def CMD_READ_LIB(self):
        self.send_command(self.cmd_seq, commands.CMD_READ_LIB.signature)
155 156
        return WHAT

157 158 159 160 161 162 163 164 165 166 167 168 169 170
    def CMD_READ_PTP(self):
        self.send_command(self.cmd_seq, commands.CMD_READ_PTP.signature)
        res = self.wait_response(COMMAND_RECEIVED)
        print("... COMMAND_RECEIVED: %s"%res)
        res = self.wait_response(RESP_READ_PTP)
        print("... RESP_READ_PTP: %s"%res)
        return res.val

    def CMD_LCK_TL(self):
        self.send_command(self.cmd_seq, commands.CMD_LCK_TL.signature)
        res = self.wait_response(COMMAND_RECEIVED)
        print("... COMMAND_RECEIVED: %s"%res)
        res = self.wait_response(RESP_ACK)
        print("... RESP_ACK: %s"%res)
171 172
        return GBS_OK

173 174 175 176 177 178
    def CMD_UNLCK_TL(self):
        self.send_command(self.cmd_seq, commands.CMD_UNLCK_TL.signature)
        res = self.wait_response(COMMAND_RECEIVED)
        print("... COMMAND_RECEIVED: %s"%res)
        res = self.wait_response(RESP_ACK)
        print("... RESP_ACK: %s"%res)
179
        return GBS_OK
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

    def CMD_NAIL_LIN(self, id, bstop, start, finish, speed, acc):
        self.send_command(dict(seq=self.cmd_seq,
                               id=id,
                               bstop=bstop,
                               S_Ac=dict(speed=speed, acc=acc),
                               start=start,
                               finish=finish),
                          commands.CMD_NAIL_LIN.signature)
        res = self.wait_response(COMMAND_RECEIVED)
        print("... COMMAND_RECEIVED: %s"%res)
        res = self.wait_response(RESP_ACK)
        print("... RESP_ACK: %s"%res)
        res = self.wait_response(RESP_ACK)
        print("... RESP_ACK: %s"%res)
195
        return GBS_OK
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

    def CMD_MOVE_MEASURE(self, id, coordinate, angle, speed, acc):
        self.send_command(dict(seq=self.cmd_seq,
                               id=id,
                               pos=dict(pos=coordinate, rot=angle),
                               S_Ac=dict(speed=speed, acc=acc)),
                          commands.CMD_MOVE_MEASURE.signature)
        res = self.wait_response(COMMAND_RECEIVED)
        print("... COMMAND_RECEIVED: %s"%res)
        res = self.wait_response(RESP_ACK)
        print("... RESP_ACK: %s"%res)
        res = self.wait_response(RESP_ACK)
        print("... RESP_ACK: %s"%res)
        res = self.wait_response(RESP_MOVE_MEASURE)
        print("... RESP_MOVE_MEASURE: %s"%res)
211 212
        ans = dict((k, res[k]) for k in ('x1', 'x2', 'y1'))
        return ans
213 214 215 216 217 218 219 220 221 222 223 224 225 226

    def CMD_MOVE_ARC(self, posI, posF, speed, acc):
        self.send_command(dict(seq=self.cmd_seq,
                               cmd=dict(
                                   posI=posI,
                                   posF=posF,
                                   S_Ac=dict(speed=speed, acc=acc))),
                          commands.CMD_MOVE_ARC.signature)
        res = self.wait_response(COMMAND_RECEIVED)
        print("... COMMAND_RECEIVED: %s"%res)
        res = self.wait_response(RESP_ACK)
        print("... RESP_ACK: %s"%res)
        res = self.wait_response(RESP_ACK)
        print("... RESP_ACK: %s"%res)
227 228
        return GBS_OK

229 230 231 232 233 234

#TODO
    def CMD_MOVE(self, moves):
        self.send_command(dict(seq=self.cmd_seq,
                               cmd=moves),
                          commands.CMD_MOVE.signature)
235 236
        return GBS_OK

237 238 239 240 241

    def run_one(self):
        self.d.runOne();
        return self.run

242 243 244 245
    def receiver(self):
        run = True
        while run:
            run = self.run_one()
246 247

if __name__ == "__main__":
248

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    port = sys.argv[1] if len(sys.argv) >= 2 else 9876
    version = sys.argv[2] if len(sys.argv) == 3 else "LabComm2014.v1"
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.connect((socket.gethostname(), port))

    client = test_client(server)
    t = threading.Thread(target=receiver, args=(client,))
    t.start()

    try:
        client.CMD_UNLCK_TL()
        client.CMD_LCK_TL()

        coord = dict(x=0,y=1,z=2)
        rot = dict(c=0,b=0,a=0)
        client.CMD_MOVE_LIN(coord, rot, 1, 0.1)

        client.CMD_MOVE_PTP(range(6), 1, 0.1)

        client.CMD_READ_PTP()

        coord = dict(x=0,y=1,z=2)
        rot = dict(c=0,b=0,a=0)
        client.CMD_MOVE_MEASURE(1, coord, rot, 1, 0.1)

        start = dict(pos=dict(x=0,y=1,z=2),
                     rot=dict(c=0,b=0,a=0))
        finish = dict(pos=dict(x=0,y=1,z=2),
                      rot=dict(c=0,b=0,a=0))
        client.CMD_NAIL_LIN(1, False, start, finish, 1, 0.1)

        posI = dict(pos=dict(x=0,y=1,z=2),
                     rot=dict(c=0,b=0,a=0))
        posF = dict(pos=dict(x=0,y=1,z=2),
                      rot=dict(c=0,b=0,a=0))
        client.CMD_MOVE_ARC(posI, posF, 1, 0.1)

        #linear
        move1 = dict(pos=dict(pos=dict(x=0,y=1,z=2),
                              rot=dict(c=0,b=0,a=0)),
                     S_Ac=dict(speed=1,
                               acc=2))

        move2 = dict(pos=dict(pos=dict(x=5,y=1,z=0),
                              rot=dict(c=0,b=0,a=0)),
                     S_Ac=dict(speed=1,
                               acc=2))



        #ptp
        move3 = dict(j=[0, 0, 0, 0, 0, 0],
                     S_Ac = dict (speed=0.1, acc=0.5))

        #arc
        move4 = dict(posI=dict(pos=dict(x=2,y=4,z=5),
                               rot=dict(c=0,b=0,a=1)),
                     posF=dict(pos=dict(x=0,y=1,z=0.5),
                               rot=dict(c=0,b=1,a=0)),
                     S_Ac = dict(speed=0.4, acc=1))

        #example of how to use variable length lists to make a "union"

        moves = [dict(move_lin=[move1],move_ptp=[],move_arc=[]),
                 dict(move_lin=[move2],move_ptp=[],move_arc=[]),
                 dict(move_lin=[],move_ptp=[move3],move_arc=[]),
                 dict(move_lin=[],move_ptp=[],move_arc=[move4]),]

        client.CMD_MOVE(moves)

        client.shutdown()

    except EOFError:
        print "got EOF"
    except Exception, e:
        print "got Exception"
        print e
        traceback.print_exc()

class ROS_client(test_client):

#GBSPose p
#float32 s
#float32 ac
#---
#float32 err1
#float32 err2

    def handle_gbs_linmove(self, req):
        ans = GBSLinMoveResponse()
        coordinate = req.p.pos
        angle = req.p.ori
        speed = req.s
        acc = req.ac
        try:
            res = self.CMD_MOVE_LIN(self, req.p.pos, req.p.ori, req.s, req.ac)
            ans.err1 = 0 # XXX
            ans.err2 = 0 # XXX
        except:
            ans.err1 = GBS_ERROR
            ans.err2 = 0 # XXX
        return ans

# GBSJointPose p
# float32 s
# float32 ac
# ---
# float32 err1
# float32 err2
    def handle_gbs_ptpmove(self, req):
        ans = GBSPTPMoveResponse()
        j = [req.p.j1, req.p.j2, req.p.j3, req.p.j4, req.p.j5, req.p.j6]
        try:
            res = self.CMD_MOVE_PTP(j, req.s, req.ac)
            ans.err1 = 0 # XXX
            ans.err2 = 0 # XXX
        except:
            err1 = GBS_ERROR
            err2 = 0 # XXX
        return ans
#
# ---
#float32 err
#GBSJointPose p
    def handle_gbs_readcurjointpose(self, req):
        ans = GBSReadCurJointPoseResponse()
        try:
            res = self.CMD_READ_PTP()
            ans.err = 0 # XXX
            ans.p.j1 = res[0]
            ans.p.j2 = res[1]
            ans.p.j3 = res[2]
            ans.p.j4 = res[3]
            ans.p.j5 = res[4]
            ans.p.j6 = res[5]
        except:
            ans.err = GBS_ERROR
        return ans
387
#
388 389 390 391 392 393 394 395 396 397
# ---
# float32 err
    def handle_gbs_locktool(self, req):
        ans = GBSLockToolResponse()
        try:
            res = self.CMD_LCK_TL()
            ans.err = 0 # XXX
        except:
            ans.err = GBS_ERROR
        return ans
398
#
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
# ---
# float32 err
    def handle_gbs_unlocktool(self, req):
        ans = GBSUnlockToolResponse()
        try:
            res = self.CMD_UNLCK_TL()
            ans.err = 0 # XXX
        except:
            ans.err = GBS_ERROR
        return ans

# GBSPose pi
# GBSPose pf
# float32 s
# float32 ac
# ---
# float32 err1
# float32 err2
    def handle_gbs_circmove(self, req):
        posI = dict(pos=req.pi.pos, rot=req.pi.ori)
        posF = dict(pos=req.pf.pos, rot=req.pf.ori)
        ans = GBSCircMoveResponse()
        try:
            res = CMD_MOVE_ARC(posI, posF, req.s, req.ac)
            ans.err1 = 0 # XXX
            ans.err2 = 0 # XXX
        except:
            ans.err1 = GBS_ERROR
            ans.err2 = GBS_ERROR
        return ans

# TODO
    def handle_gbs_combmove(self, req):
        raise NotImplementedError()

# int32 id
# GBSPose p
# float32 s
# float32 ac
# ---
# float32 err1
# float32 err2
    def handle_gbs_measuremove(self, req):
        ans = GBSMeasureMoveResponse()
        coord = req.p.pos
        angle = req.p.ori
        try:
            res = CMD_MOVE_MEASURE(req.id, coord, angle, req.s, req.ac)
            ans.err1 = 0 #XXX
            ans.err2 = 0 #XXX
        except:
            ans.err1 = GBS_ERROR
            ans.err2 = GBS_ERROR
        return ans

# int32 id
# bool bstop
# float32 s
# float32 ac
# GBSPose p1
# GBSPose p2
# ---
# float32 err1
# float32 err2
    def handle_gbs_naillinmove(self, req):
        ans = GBSNailLinMoveResponse()
        start = dict(pos=req.p1.pos, rot=req.p1.ori)
        finish = dict(pos=req.p2.pos, rot=req.p2.ori)
        try:
            res = CMD_NAIL_LIN(req.id, req.bstop, start, finish, req.s, req.ac)
            ans.err1 = 0 #XXX
            ans.err2 = 0 #XXX
        except:
            ans.err1 = GBS_ERROR
            ans.err2 = GBS_ERROR
        return ans