In [1]:
import socket
import sys
from collections import namedtuple
from struct import unpack
import asyncore
import rospy
from std_msgs.msg import Float32
from std_msgs.msg import String
from std_srvs.srv import Trigger, SetBool
import std_srvs.srv
In [2]:
Message = namedtuple('Message', ['position1','speed1','torque1','position2','speed2','torque2','first_letter','second_letter'])
rospy.init_node('ROS_PLC_bridge')
class HTTPClient(asyncore.dispatcher):
def __init__(self, host="192.168.1.120", port=8000, message_size=50):
asyncore.dispatcher.__init__(self)
self.message_size=message_size #message_size=50
self.create_socket()
self.connect( (host, port) ) #connect to port 8000, host 192.168.1.120
self.buffer = bytes('testing','utf8')
self.counter=0
self.last_message=""
self.s_start = rospy.Service('start_stop', SetBool, self.start_stop_communication)
self.s_mode = rospy.Service('change_mode', SetBool, self.change_mode)
def start_stop_communication(self, message, host="192.168.1.120", port=8000, message_size=50):
'''this methond allows or stops communication between ROS and the PLC'''
if message.data:
print("Establishing communication")
self.connect( (host, port) ) #connect to port 8000, host 192.168.1.120
return std_srvs.srv.SetBoolResponse(1, "Communication established")
else:
print("Shutting down communication")
#self.shutdown(self.SHUT_RDWR)
self.close()
self.create_socket()
return std_srvs.srv.SetBoolResponse(1, "Communication shutdown")
def change_mode(self,message):
if message.data:
return std_srvs.srv.SetBoolResponse(1, "Mode changed to option 1")
else:
return std_srvs.srv.SetBoolResponse(0, "Mode changed to option 0")
def handle_connect(self):
#print("connected")
pass
def decode_message(self,message):
'''this method decodes the message received through TCP/IP'''
if(len(message)==self.message_size):
touple_from_tcp=unpack('ddddddcc', message)
my_message=Message(*touple_from_tcp)
return my_message
return False
def handle_read(self):
'''this method receives a message through TCP/IP and sends it to ROS'''
byte_message=self.recv(self.message_size)
self.last_message=self.decode_message(byte_message)
if(self.last_message):
try:
pass
except:
print(":-(")
self.counter+=1
In [3]:
client = HTTPClient('192.168.1.120',8000)
asyncore.loop()
In [ ]:
In [ ]:
asyncore.loop()
In [ ]:
#received_message=sock.recv(50)
#print(received_message)
In [ ]:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
ip="192.168.1.120"
port=8000
# Connect the socket to the port where the server is listening
server_address = (ip,port)
print( 'connecting to %s port %s' % server_address)
somesocket=sock.connect(server_address)
#sock.sendall("testing".encode())
received_message=sock.recv(50)
In [ ]:
print(len(received_message))
In [ ]:
touple_from_tcp=unpack('ddddddcc', received_message)
touple_from_tcp
In [ ]:
Message = namedtuple('Message', ['position1','speed1','torque1','position2','speed2','torque2','first_letter','second_letter'])
my_message=Message(*touple_from_tcp)
list(my_message)
In [ ]:
my_message.position1
In [ ]:
client.socket.close()
In [ ]:
client.counter
In [ ]:
#!/usr/bin/env python
import rospy
def start_stop_communication(inside):
#start stop rail
print("inside service response")
print(inside)
return std_srvs.srv.SetBoolResponse(1,"we have suceeded")
In [ ]:
? std_srvs.srv.SetBoolResponse
In [ ]:
s_start.shutdown()
In [ ]:
s_start = rospy.Service('start_stop4', SetBool, start_stop_communication)
In [ ]:
trigger_experiment = rospy.ServiceProxy('start_stop4', SetBool)
In [ ]:
response=trigger_experiment(True)
In [ ]:
print(response.message)
print(response.success)
In [ ]:
sth=Trigger()
In [ ]:
print(sth.)
In [ ]:
? sth
In [ ]: