mirror of
https://github.com/jjrobots/B-ROBOT_EVO2.git
synced 2026-02-21 20:01:22 +01:00
New version of BROBOT EVO2
This is the first oficial version of BROBOT EVO2. Enjoy!
This commit is contained in:
66
Blockly/python/server/BROBOT_Class.py
Normal file
66
Blockly/python/server/BROBOT_Class.py
Normal file
@@ -0,0 +1,66 @@
|
||||
# BROBOT PYTHON CLASS
|
||||
# Exampe 1: Simple commands
|
||||
# Before running the script you need to connect the PC to the BROBOT wifi
|
||||
# Remember, default password for Wifi network JJROBOTS_XX is 87654321
|
||||
|
||||
# author: JJROBOTS 2016
|
||||
# version: 1.01 (27/10/2016)
|
||||
# Licence: Open Source (GNU LGPLv3)
|
||||
|
||||
import socket
|
||||
import time
|
||||
import struct
|
||||
|
||||
# CLASS to control BROBOT
|
||||
class BROBOT(object):
|
||||
UDP_IP = "192.168.4.1" # Default BROBOT IP (with BROBOT JJROBOTS_XX wifi)
|
||||
UDP_PORT = 2222 # Default BROBOT port
|
||||
sock = 0
|
||||
def __init__(self):
|
||||
# Create default socket with UDP protocol
|
||||
self.sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
|
||||
|
||||
def sendCommand(self,OSCmessage,param):
|
||||
base = bytearray(OSCmessage) # OSC message
|
||||
param = bytearray(struct.pack(">f",param))
|
||||
message = base+param
|
||||
self.sock.sendto(message,(self.UDP_IP,self.UDP_PORT))
|
||||
time.sleep(0.05)
|
||||
|
||||
# Mode: 0 NORMAL MODE, 1 PRO MODE
|
||||
def mode(self,value=0.0):
|
||||
print "BROBOT Mode:",value
|
||||
if (value!=1): value = 0.0
|
||||
# Encapsulate the commands on OSC protocol UDP message and send...
|
||||
self.sendCommand(b'/1/toggle1/\x00\x00,f\x00\x00',value)
|
||||
|
||||
# Throttle command. Values from [-1.0 to 1.0] positive: forward
|
||||
def throttle(self,value=0.0):
|
||||
print "BROBOT Throttle:",value
|
||||
value = (value+1.0)/2.0 # Adapt values to 0.0-1.0 range
|
||||
self.sendCommand(b'/1/fader1\x00\x00\x00,f\x00\x00',value) #send OSC message
|
||||
|
||||
# Steering command. Values from [-1.0 to 1.0] positive: turn right
|
||||
def steering(self,value=0.0):
|
||||
print "BROBOT Steering:",value
|
||||
value = (value+1.0)/2.0 # Adapt values to 0.0-1.0 range
|
||||
self.sendCommand(b'/1/fader2\x00\x00\x00,f\x00\x00',value) #send OSC message
|
||||
|
||||
# Move speed, steps1, steps2
|
||||
def move(self,speed,steps1,steps2):
|
||||
print "BROBOT MOVE",speed,steps1,steps2
|
||||
base = bytearray(b'/1/move\x00\x00\x00')
|
||||
param1 = bytearray(struct.pack("h",speed))
|
||||
param2 = bytearray(struct.pack("h",steps1))
|
||||
param3 = bytearray(struct.pack("h",steps2))
|
||||
message = base+param1+param2+param3
|
||||
self.sock.sendto(message,(self.UDP_IP,self.UDP_PORT))
|
||||
#print len(message)
|
||||
#self.sendCommand(b'/1/move\x00\x00\x00\x00\x00\x00
|
||||
|
||||
# Servo command. Values 0 or 1 (activated)
|
||||
def servo(self,value=0.0):
|
||||
print "BROBOT Servo:",value
|
||||
if (value!=1):value=0.0
|
||||
self.sendCommand(b'/1/push1\x00\x00,f\x00\x00',value) #send OSC message
|
||||
|
||||
BIN
Blockly/python/server/BROBOT_Class.pyc
Normal file
BIN
Blockly/python/server/BROBOT_Class.pyc
Normal file
Binary file not shown.
183
Blockly/python/server/webserver.py
Normal file
183
Blockly/python/server/webserver.py
Normal file
@@ -0,0 +1,183 @@
|
||||
#!/usr/bin/python
|
||||
# Blockly interface for BROBOT block programming
|
||||
|
||||
# Remember to connect your wifi to your robot network JJROBOTS_XX password:8764321
|
||||
|
||||
# author: JJROBOTS 2017
|
||||
# version: 1.01 (10/03/2017)
|
||||
# Licence: Open Source (GNU LGPLv3)
|
||||
|
||||
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
|
||||
from os import curdir, sep
|
||||
import urlparse
|
||||
from BROBOT_Class import BROBOT
|
||||
import time
|
||||
import webbrowser
|
||||
import threading
|
||||
import socket
|
||||
|
||||
# BROBOT initialization
|
||||
myRobot = BROBOT()
|
||||
IP = myRobot.UDP_IP
|
||||
PORT = myRobot.UDP_PORT
|
||||
|
||||
HTTP2UDP_PORT = 8008
|
||||
PORT_NUMBER = 8080
|
||||
|
||||
# Telemetry port
|
||||
UDP_PORT = 2223
|
||||
|
||||
# Global variables
|
||||
global status
|
||||
global angle
|
||||
status = 0
|
||||
angle = 0.0
|
||||
|
||||
# Class to launch servers on different threads
|
||||
class ServerThread(threading.Thread):
|
||||
def __init__(self,port,handler):
|
||||
threading.Thread.__init__(self)
|
||||
self.port=port
|
||||
self.handler = handler
|
||||
|
||||
def run(self):
|
||||
server = HTTPServer(('localhost', self.port), self.handler)
|
||||
server.serve_forever()
|
||||
|
||||
|
||||
#Browser to handle HTTP to UDP conversion
|
||||
class HTTP2UDP(BaseHTTPRequestHandler):
|
||||
# Override address_string to avoid using name lookup
|
||||
def address_string(self):
|
||||
host, port = self.client_address[:2]
|
||||
#return socket.getfqdn(host)
|
||||
return host
|
||||
def do_GET(self):
|
||||
#print("GET request")
|
||||
self.send_response(200)
|
||||
self.send_header("Access-Control-Allow-Origin","*")
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
params = self.path
|
||||
params = params.split('?')
|
||||
if (len(params)>1):
|
||||
params = urlparse.parse_qs(params[1])
|
||||
if 'IP' in params.keys():
|
||||
print "IP: "+params['IP'][0]
|
||||
myRobot.UDP_IP = params['IP'][0]
|
||||
if 'PORT' in params.keys():
|
||||
print "PORT: "+params['PORT'][0]
|
||||
myRobot.UDP_PORT = int(params['PORT'][0])
|
||||
if 'ST' in params.keys():
|
||||
#print params['ST'][0]
|
||||
myRobot.steering(int(params['ST'][0])/100.0)
|
||||
if 'TH' in params.keys():
|
||||
#print params['TH'][0]
|
||||
myRobot.throttle(int(params['TH'][0])/100.0)
|
||||
if 'MV' in params.keys():
|
||||
mvparams = params['MV'][0].split(",")
|
||||
#print mvparams[0]+" "+mvparams[1]+" "+mvparams[2]
|
||||
myRobot.move(int(mvparams[0]),int(mvparams[1]),int(mvparams[2]))
|
||||
if 'MO' in params.keys():
|
||||
#print params['MO'][0]
|
||||
myRobot.mode(int(params['MO'][0]))
|
||||
if 'SE' in params.keys():
|
||||
#print params['SE'][0]
|
||||
myRobot.servo(int(params['SE'][0]))
|
||||
if 'RS' in params.keys():
|
||||
self.wfile.write(status)
|
||||
return
|
||||
if 'RA' in params.keys():
|
||||
self.wfile.write(angle)
|
||||
return
|
||||
self.wfile.write('OK')
|
||||
return
|
||||
|
||||
def log_request(self, code=None, size=None):
|
||||
pass # Do nothing...
|
||||
#print('Request')
|
||||
|
||||
|
||||
|
||||
#This class will handles any incoming request from
|
||||
#the browser
|
||||
class myHandler(BaseHTTPRequestHandler):
|
||||
|
||||
#Handler for the GET requests
|
||||
def do_GET(self):
|
||||
if self.path=="/":
|
||||
self.path="brobot/index.html"
|
||||
|
||||
try:
|
||||
#Check the file extension required and
|
||||
#set the right mime type
|
||||
|
||||
sendReply = False
|
||||
binary = False
|
||||
if self.path.endswith(".html"):
|
||||
mimetype='text/html'
|
||||
sendReply = True
|
||||
if self.path.endswith(".jpg"):
|
||||
binary = True
|
||||
mimetype='image/jpg'
|
||||
sendReply = True
|
||||
if self.path.endswith(".png"):
|
||||
binary = True
|
||||
mimetype='image/png'
|
||||
sendReply = True
|
||||
if self.path.endswith(".cur"):
|
||||
binary = True
|
||||
mimetype='image/x-win-bitmap'
|
||||
sendReply = True
|
||||
if self.path.endswith(".wav"):
|
||||
binary = True
|
||||
mimetype='audio/wav'
|
||||
sendReply = True
|
||||
if self.path.endswith(".mp3"):
|
||||
binary = True
|
||||
mimetype='audio/mpeg'
|
||||
sendReply = True
|
||||
if self.path.endswith(".gif"):
|
||||
binary = True
|
||||
mimetype='image/gif'
|
||||
sendReply = True
|
||||
if self.path.endswith(".js"):
|
||||
mimetype='application/javascript'
|
||||
sendReply = True
|
||||
if self.path.endswith(".css"):
|
||||
mimetype='text/css'
|
||||
sendReply = True
|
||||
|
||||
if sendReply == True:
|
||||
#Open the static file requested and send it
|
||||
if binary:
|
||||
f = open(curdir + sep + self.path,'rb')
|
||||
else:
|
||||
f = open(curdir + sep + self.path)
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type',mimetype)
|
||||
self.end_headers()
|
||||
self.wfile.write(f.read())
|
||||
f.close()
|
||||
return
|
||||
|
||||
except IOError:
|
||||
self.send_error(404,'File Not Found: %s' % self.path)
|
||||
|
||||
try:
|
||||
#Create the HTTP to UDP server to send messages to robot
|
||||
ServerThread(HTTP2UDP_PORT,HTTP2UDP).start()
|
||||
#Create a web server and define the handler to manage the incoming requests
|
||||
ServerThread(PORT_NUMBER,myHandler).start()
|
||||
print "Started HTTP2UDP server on port ", HTTP2UDP_PORT
|
||||
print "Started httpserver on port " , PORT_NUMBER
|
||||
print "Opening browser... wait a moment..."
|
||||
time.sleep(2)
|
||||
url = "http://localhost:8080/brobot/index.html"
|
||||
webbrowser.open(url,new=2)
|
||||
while 1:
|
||||
pass
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print '^C received, shutting down the web server'
|
||||
exit()
|
||||
Reference in New Issue
Block a user