Advertisement
BourbonCreams

moving_braccio_pc.py

Jul 21st, 2017
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. import sys
  2. import requests
  3. import json
  4.  
  5. def main(values):
  6.     # If the value for the movement is given, use it. Otherwise keep Braccio steady.
  7.     if len(values) == 7:
  8.  
  9.         values.pop(0)
  10.         # Convert string to int
  11.         intValues = []
  12.         try:
  13.             for value in values:
  14.                 intValues.append(int(float(value)))
  15.  
  16.  
  17.         except ValueError:
  18.             print "Expecting an int..."
  19.  
  20.     else:
  21.         print "Expecting 6 values for the servos, " + str(len(values)-1) + " received. Command ignored."
  22.         intValues = [0]
  23.  
  24.     print intValues
  25.     if (intValues[0] < 0 or intValues[0] > 180 or intValues[1] < 15 or intValues[1] > 165 or
  26.         intValues[2] < 0 or intValues[2] > 180 or intValues[3] < 0 or intValues[3] > 180 or
  27.         intValues[4] < 0 or intValues[4] > 180):
  28.  
  29.         print ("\nNot every value is in the accepted range. \n\nM1=base degrees. Allowed "
  30.               "values from 0 to 180 degrees\nM2=shoulder degrees. Allowed values from 15 to "
  31.               "165 degrees \nM3=elbow degrees. Allowed values from 0 to 180 degrees \nM4=wrist "
  32.               "vertical degrees. Allowed values from 0 to 180 degrees \nM5=wrist rotation "
  33.               "degrees. Allowed values from 0 to 180 degrees\nM6=gripper degrees. Allowed "
  34.               "values from 10 to 73 degrees. 10: the tongue is open, 73: the gripper is "
  35.               "closed.")
  36.  
  37.     url = "http://192.168.1.129:2883/jsonrpc"
  38.     headers = {'content-type': 'application/json'}
  39.     if (len(intValues) > 1):
  40.         # Example echo method
  41.         payload = {
  42.             "method": "moving_braccio",
  43.             "params": {"M1": intValues[0], "M2": intValues[1], "M3": intValues[2],
  44.                "M4": intValues[3], "M5": intValues[4], "M6": intValues[5]},
  45.             "jsonrpc": "2.0",
  46.             "id": 0,
  47.         }
  48.     else:
  49.         # Example echo method
  50.         payload = {
  51.             "method": "moving_braccio",
  52.             "params": {"M6": 0},
  53.             "jsonrpc": "2.0",
  54.             "id": 0,
  55.         }
  56.  
  57.     try:
  58.         response = requests.post(
  59.             url, data=json.dumps(payload), headers=headers).json()
  60.         print response
  61.         return response["result"]
  62.     except requests.ConnectionError:
  63.         print "Connection error. Is moving_braccio_arduino.py running?"
  64.  
  65.  
  66.  
  67. if __name__ == "__main__":
  68.     main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement