-1

I'm building a robot and need to control it remotely. I've decided to do this by using zeromq. From my computer (server) I will send x- and y-coordinates to the robot(client). I therefore need the message to be sent as numbers so I can make the robot go to the coordinates. How do i do this? I am very new to programming (as you can probably tell) I currently have this code:

client

import zmq
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://127.0.0.1:5555")
while True:
        socket.send_string("Robot Ready")
        coordinates= socket.recv_string()
        print("From server",coordinates)
#And here i want to use the received coordinates to give the robot commands#

server

import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://127.0.0.1:5555")

while True:
    msg = socket.recv()
    print(msg)
    smsg = input("Enter coordinates : ")
    socket.send_string(smsg)`

I updated my code to this (it works but i feel like it's unnecessary long):

client2

import zmq
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://127.0.0.1:5555")

while True:
        socket.send_string("AGV Ready")
        x_start = float(socket.recv())
        socket.send_string("X-coordinate registred")
        y_start = float(socket.recv())
        socket.send_string('Y-coordinate registred')
        x_end = float(socket.recv())
        socket.send_string("X-coordinate registred")
        y_end = float(socket.recv())
        print("Start position: ",x_start, y_start)
        print("End position: ", x_end, y_end)

server2

import zmq
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://127.0.0.1:5555")

while True:
    msg = socket.recv()
    print(msg)
    smsg = input("Pick up product at x,y : ")
    smsg_new = smsg.split(',')
    socket.send_string(smsg_new[0])
    socket.recv()
    socket.send_string(smsg_new[1])
    socket.recv()
    smsg2 = input("Leave product at x,y : ")
    smsg2_new = smsg2.split(',')
    socket.send_string(smsg2_new[0])
    socket.recv()
    socket.send_string(smsg2_new[1])
Community
  • 1
  • 1
Bjokle
  • 1
  • 1
  • 1
    go ahead and try it yourself first. you have no question thus-far if you don't try. See docs [here](https://realpython.com/python-sockets/) and [here](https://docs.python.org/3/howto/sockets.html) on how to communicate information. Once you wrote remaining code and keep hitting a bump then you can ask this question and update it with errorcode. Now your question is too broad.. and asking for a manual. – ZF007 Apr 01 '19 at 12:37
  • When the user is prompted to `Enter coordinates : `, what exactly are they supposed to type? for example, "x=23, y=42", or "23, 42", or "(23, 42)", or "23" followed by "42" in the next prompt? Or something else? – Kevin Apr 01 '19 at 12:37
  • @Kevin The user is supposed to type start-coordinates, "x,y" and then end-coordinates "x,y" – Bjokle Apr 02 '19 at 09:10

3 Answers3

1

I assume the input is expected to be integer. So I'd suggest

socket.send_string(int(smsg))

Edit:

What's the expected format? Should the X and Y coordinates be separated by comma, etc?

If you are sending both X and Y at the same time, you should restrict the user input to be in the following format:

<X-coordinates>, <Y-coordinates>

Then split the string:

xy = smsg.split(',')
socket.send_string((int(xy[0]), int(xy[1]))
Quy Vu Xuan
  • 159
  • 1
  • 5
  • 13
  • Adhering DRY: `x, y = map(int, smsg.split(','))` – DeepSpace Apr 01 '19 at 12:40
  • I tried this but I got this error: "TypeError: unicode/str objects only". I updated my code and it does what I want it to do, but I feel like it's unnecessary many number of lines. – Bjokle Apr 02 '19 at 09:11
0

I presume that your coordinates will be in the form "x y". To parse your coordinates, you'll need a split() and a cast.

Add this to your client code:

sp = coordinates.split(' ') #This splits the string into a string array
                            #Using the specified delimiter
x = float(sp[0]) #This float() command converts the string into an number
y = float(sp[1])
print("X Cordinate: " + str(x))
print("Y Cordinate: " + str(y))

If your coordinates are comma-delimited, then simply use split(',') instead.

EDIT

Per suggestion in the comments, The casting can indeed be simplified to

x, y = map(float, coordinates.split(' '))

You can read a little about map() here. However, I would still recommend additional reading on python casting. It is a fundamental skill that should not be ignored.

armitus
  • 712
  • 5
  • 20
  • 1. Use `format` so you don't have to convert back and forth. 2. This can be turned into a single-line, `x, y = map(float, coordinates.split())` – DeepSpace Apr 01 '19 at 12:39
  • I tried this but got this message: "TypeError: unicode/str objects only". I updated my code but i feel like it's unnecessary many lines – Bjokle Apr 02 '19 at 09:04
  • In your client2 example, you're using socket.recv(), which returns a byte array (https://docs.python.org/3/library/socket.html?highlight=socket%20recv#socket.socket.recv). You'll need to cast the byte array into a string. See https://stackoverflow.com/questions/13979764/python-converting-sock-recv-to-string – armitus Apr 02 '19 at 11:49
  • but considering that you're using `zmq`, I would recommend that you simply send/receive with `send_string` like you did in your first example. This should automatically translate. See https://pyzmq.readthedocs.io/en/latest/api/zmq.html – armitus Apr 02 '19 at 11:54
-1
try:
    value = int(strvalue)
except ValueError:
    print("Failed to convert str to int.")
w1kl4s
  • 127
  • 2
  • 12
  • Since the data contains both x and y coordinates, it seems unlikely to me that the user will enter a single number into the prompt. – Kevin Apr 01 '19 at 12:38
  • 2
    While this code may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. – Busti Apr 01 '19 at 15:38