Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import signal
  4. import json
  5. import flicklib
  6. import time
  7. import paho.mqtt.client as mqtt
  8. import curses
  9. from curses import wrapper
  10.  
  11. some_value = 5000
  12.  
  13. @flicklib.move()
  14. def move(x, y, z):
  15.     global xyztxt
  16.     xyztxt = '{:5.3f} {:5.3f} {:5.3f}'.format(x,y,z)
  17.  
  18. @flicklib.flick()
  19. def flick(start,finish):
  20.     global flicktxt
  21.     flicktxt = start + ' - ' + finish
  22.  
  23. @flicklib.airwheel()
  24. def spinny(delta):
  25.     global some_value
  26.     global airwheeltxt
  27.     some_value += delta
  28.     if some_value < 0:
  29.         some_value = 0
  30.     if some_value > 10000:
  31.         some_value = 10000
  32.     airwheeltxt = str(some_value/100)
  33.  
  34. @flicklib.double_tap()
  35. def doubletap(position):
  36.     global doubletaptxt
  37.     doubletaptxt = position
  38.  
  39. @flicklib.tap()
  40. def tap(position):
  41.     global taptxt
  42.     taptxt = position
  43.  
  44. @flicklib.touch()
  45. def touch(position):
  46.     global touchtxt
  47.     touchtxt = position
  48.  
  49.  
  50. def mqttConnect(user, password, url, port):
  51.     client = mqtt.Client()
  52.     client.username_pw_set(user, password)
  53.     client.connect(url, port)
  54.     return client
  55.    
  56. def sendFlickCommand(command, client):
  57.     client.publish("tove", "method")
  58.     if command == "north - south":
  59.         joint = "footVal"
  60.         rotation = 0
  61.     elif command == "south - north":
  62.         joint = "footVal"
  63.         rotation = 180
  64.     elif command == "east - west":
  65.         joint = "wristVal"
  66.         rotation = 0
  67.     elif command == "west - east":
  68.         joint = "wristVal"
  69.         rotation = 180
  70.     elif command == "open":
  71.         joint = "handVal"
  72.         rotation = 20
  73.     elif command == "close":
  74.        
  75.         joint = "handVal"
  76.         rotation = 118
  77.     else:
  78.         joint = command
  79.         rotation = command
  80.  
  81.    
  82.     data = {joint:[rotation]}
  83.     client.publish("inputMsg", json.dumps(data))
  84.    
  85.    
  86. #
  87. # Main display using curses
  88. #
  89.  
  90. def main(stdscr):
  91.     global xyztxt
  92.     global flicktxt
  93.     global airwheeltxt
  94.     global touchtxt
  95.     global taptxt
  96.     global doubletaptxt
  97.     global client
  98.     global exittxt
  99.    
  100.     mqttUser = "lrgyidry"
  101.     mqttPassword = "gkpK0xc5L264"
  102.     mqttUrl = "m21.cloudmqtt.com"
  103.     mqttPort = 17240
  104.    
  105.  
  106.     xyztxt = ''
  107.     flicktxt = ''
  108.     flickcount = 0
  109.     airwheeltxt = ''
  110.     airwheelcount = 0
  111.     touchtxt = ''
  112.     touchcount = 0
  113.     taptxt = ''
  114.     tapcount = 0
  115.     doubletaptxt = ''
  116.     doubletapcount = 0
  117.     client = mqttConnect(mqttUser, mqttPassword, mqttUrl, mqttPort)
  118.    
  119.     client.publish("inputMsg", "tove")
  120.     # Clear screen and hide cursor
  121.     stdscr.clear()
  122.     curses.curs_set(0)
  123.  
  124.     # Add title and footer
  125.     exittxt = 'Implemented: flick direction, tab to open, doubletab to close'
  126.     title = '**** ROBO ARM CONTROLLER 2000 ****'
  127.     stdscr.addstr( 0, (curses.COLS - len(title)) / 2, title)
  128.     stdscr.addstr(22, (curses.COLS - len(exittxt)) / 2, exittxt)
  129.     stdscr.refresh()
  130.  
  131.     fw_info = flicklib.getfwinfo()
  132.  
  133.     datawin = curses.newwin( 8, curses.COLS - 6,  2, 3)
  134.     fwwin   = curses.newwin(10, curses.COLS - 6, 11, 3)
  135.  
  136.     # Fill firmware info window.
  137.     fwwin.erase()
  138.     fwwin.border()
  139.     fwwin.addstr(1, 2, 'Firmware valid: ' + 'Yes' if fw_info['FwValid'] == 0xaa else 'No')
  140.     fwwin.addstr(2, 2, 'Hardware Revison: ' + str(fw_info['HwRev'][0]) + '.' + str(fw_info['HwRev'][1]))
  141.     fwwin.addstr(3, 2, 'Params Start Addr: ' + '0x{:04x}'.format(fw_info['ParamStartAddr']))
  142.     fwwin.addstr(4, 2, 'Library Loader Version: ' + str(fw_info['LibLoaderVer'][0]) + '.' + str(fw_info['LibLoaderVer'][1]))
  143.     fwwin.addstr(5, 2, 'Library Loader Platform: ' + 'Hillstar' if fw_info['LibLoaderPlatform'] == 21 else 'Woodstar')
  144.     fwwin.addstr(6, 2, 'Firmware Start Addr: 0x' + '{:04x}'.format(fw_info['FwStartAddr']))
  145.     fwver_part1, fwver_part2 = fw_info['FwVersion'].split(';DSP:')
  146.     fwwin.addstr(7, 2, 'Firmware Version: ' + fwver_part1)
  147.     fwwin.addstr(8, 2, 'DSP: ' + fwver_part2)
  148.     fwwin.refresh()
  149.  
  150.     # Update data window continuously until Control-C
  151.     while True:
  152.         datawin.erase()
  153.         datawin.border()
  154.         datawin.addstr(1, 2, 'X Y Z     : ' + xyztxt)
  155.         datawin.addstr(2, 2, 'Flick     : ' + flicktxt)
  156.         datawin.addstr(3, 2, 'Airwheel  : ' + airwheeltxt)
  157.         datawin.addstr(4, 2, 'Touch     : ' + touchtxt)
  158.         datawin.addstr(5, 2, 'Tap       : ' + taptxt)
  159.         datawin.addstr(6, 2, 'Doubletap : ' + doubletaptxt)
  160.         datawin.refresh()
  161.        
  162.  
  163.         xyztxt = ''
  164.  
  165.         if len(flicktxt) > 0 and flickcount < 5:
  166.             flickcount += 1
  167.            
  168.         else:
  169.             flicktxt = ''
  170.             flickcount = 0
  171.            
  172.         if flickcount == 1:
  173.             sendFlickCommand(flicktxt, client)
  174.  
  175.         if len(airwheeltxt) > 0 and airwheelcount < 5:
  176.             airwheelcount += 1
  177.         else:
  178.             airwheeltxt = ''
  179.             airwheelcount = 0
  180.  
  181.         if len(touchtxt) > 0 and touchcount < 5:
  182.             touchcount += 1
  183.         else:
  184.             touchtxt = ''
  185.             touchcount = 0
  186.            
  187.         if len(taptxt) > 0 and tapcount < 40:
  188.             tapcount += 1
  189.         else:
  190.             taptxt = ''
  191.             tapcount = 0
  192.         if tapcount == 1:
  193.             sendFlickCommand("open", client)
  194.            
  195.         if len(doubletaptxt) > 0 and doubletapcount < 30:
  196.             doubletapcount += 1
  197.         else:
  198.             doubletaptxt = ''
  199.             doubletapcount = 0
  200.         if doubletapcount == 1:
  201.             sendFlickCommand("close", client)
  202.  
  203.  
  204.         time.sleep(0.1)
  205.  
  206. wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement