Super EZ ARC REST Server

Mickey666Maus

Germany

Thinking about how to tie the strings together, and also in terms of Unity integration... Why not use a super basic REST server, so we can send all sorts of data over to ARC?

I created a basic example, that is receiving an array of servo positions, that I am sending out from Unity. These values can be used in ARC to loop thru and create live servo position updates!

Any thoughts on how to improve this are very welcome, all types of data can be send to ARC this way, you could access a database and store different types of variables...so I guess its being a very versatile way to tie things together?

This is my first attempt to do things this way, so forgive me if the setup is super basic!!

Check if python3 is installed on your system python3 -V

if not download python https://www.python.org/downloads/

Make sure you are having Flask installed using the packet manager of choice... pip install Flask or py -m pip install Flask

Now create a file called EZ_ARC_REST.py

and copy the code into the file

from flask import Flask, json ,request

positions = [90, 100, 120, 110, 90, 90, 70, 90, 70, 90]

app = Flask(__name__)

@app.route('/set_positions/', methods=['GET','POST'])
def set_positions(position):
    global positions
    positions = position.strip('][').split(', ')
    return json.dumps(positions)

@app.route('/get_positions', methods=['GET'])
def get_positions():
    return json.dumps(positions)

@app.route('/')
def index():
  return 'ARC REST Index Page'

@app.route('/hello')
def hello():
  return 'Hello, greetings from different endpoint'

#adding variables
@app.route('/user/')
def show_user(username):
  #returns the username
  return 'Username: %s' % username

@app.route('/post/')
def show_post(post_id):
  #returns the post, the post_id should be an int
  return str(post_id)


if __name__ == '__main__':
    app.run()

Now you can save and run the file...

There are some extra endpoints so you can get the idea, what can be done with a setup like this!

Test your server by putting some endpoints to your browsers address bar... http://127.0.0.1:5000/ http://127.0.0.1:5000/hello http://127.0.0.1:5000/set_positions/[70, 110, 120, 100, 90, 80, 65, 90, 70, 60]

The last line sends a servo array to import into ARC, you can retrieve it like this

from System.Net import HttpWebRequest
uri = "http://127.0.0.1:5000/get_positions";
webRequest = HttpWebRequest.Create(uri)
response = webRequest.GetResponse()

from System.IO import StreamReader
import clr
clr.AddReference('System.Web.Extensions')
from System.Web.Script.Serialization import JavaScriptSerializer
streamReader = StreamReader(response.GetResponseStream())
jsonData = streamReader.ReadToEnd()
js = JavaScriptSerializer()
dataDict = js.DeserializeObject(jsonData)

print(dataDict)
servo =(dataDict)
for position in servo:
    print(position)

Once the array is available, you can use it to drive servo positions, or you can just use this method to get all kinds to data over to ARC...

By — Last update

ARC Pro

Upgrade to ARC Pro

Get access to the latest features and updates before they're released. You'll have everything that's needed to unleash your robot's potential!

PRO
Synthiam
#9  

What serial bus servos are you using? Because you can add the robot skill for those servos and simply set Servo.setPosition() in your code rather than writing to the servos via the COM directly with their protocol. That way your servos work across all ARC robot skills.

#10   — Edited

Hey DJ, this is just some example that I did, because ZOE is the only robot I am having assembled for testing! But of course it is also working with the ARC Servo.setPosition(), as I showed in the facemask tutorial for ARC!!

Its just one line of code to make it work with EZ-Robot HDD servos!:)

from System.Net import HttpWebRequest
uri = "http://127.0.0.1:5000/get_positions";
webRequest = HttpWebRequest.Create(uri)
response = webRequest.GetResponse()

from System.IO import StreamReader
import clr
clr.AddReference('System.Web.Extensions')
from System.Web.Script.Serialization import JavaScriptSerializer
streamReader = StreamReader(response.GetResponseStream())

def readData():
  jsonData = streamReader.ReadToEnd()
  js = JavaScriptSerializer()
  dataDict = js.DeserializeObject(jsonData)
  return dataDict

dataDict = readData()
print(dataDict)

checkMask =(dataDict)
for evaluation in checkMask:
    print(evaluation)

if evaluation == "WithoutMask":
  print("Please wear a mask!")
  Servo.SetPosition(D0, 150, 0);

if evaluation == "WithMask":
  print("Thanks for wearing a mask!")
  Servo.SetPosition(D0, 50, 0);

PRO
Synthiam
#11   — Edited

I understand the change for hdd / pwm hobby servos. But my suggestion is to use the Dx or Vx ports so it can be compatible with all existing skills that use servo controls.

If your robot uses LewanSoul servos. Then all you have to do is add the LewanSoul robot skill and configure the servo ID's to respective Vx ports. Then, your PHP code merely needs to Servo.setPosition(V0, 50); or whatever...

The advantage of using the existing Robot Skills as servo drivers allows your code to seamlessly interact with all other robot skills. When a servo position is modified in ARC, other robot skills can interact with that position change. By talking to the servo directly through your PHP code, you lose that ability for people to merge it with the ARC framework. It loses the ability to be able to work with other robot skills.

#12  

Thats a good point...I will change an test the code tonight! Will be out of town during the day!!:)

#13  

I also realized that it is quiet difficult to install flask system wide on a fresh Windows 10, you need all the Python, Pythonpath, pip, sudo, blablabla stuff... So I was also thinking of another REST Server solution...:)

PRO
Synthiam
#14  

You pretty much created a robot skill - so why not just do that?

You're already using the .net System.web.extensions and serializer. So all your program is quite basic. Again, if you're calling this from unity, not sure why you're just not using the camera servo server:D

I'll have to make a demo for you

#15  

Another good point there...I will have a look tonight!!:)

PRO
Synthiam
#16  

Ask me questions if You have any about the process