test de socket

This commit is contained in:
mpboyer 2025-01-13 17:42:12 +01:00
parent 4c71dc382b
commit cb2cace3e0
2 changed files with 47 additions and 32 deletions

View file

@ -1,5 +1,8 @@
import socket
from GCode_Interpreterdc import GCodeToMotors from GCode_Interpreterdc import GCodeToMotors
class Hardware: class Hardware:
def __init__(self): def __init__(self):
pass pass
@ -12,54 +15,58 @@ class Hardware:
class SimuHardware(Hardware): class SimuHardware(Hardware):
def __init__(self, filename): def __init__(self, bind):
try: self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.f = open(filename) self.s.setblocking(False)
except FileNotFoundError: self.s.bind(bind)
raise FileNotFoundError
def probe(self, gtm): def probe(self, gtm):
f.write("request") self.s.send("request")
encoder_x = (int)(f.readline()) self.s.listen()
encoder_y = (int)(f.readline()) while 1:
encoder_z = (int)(f.readline()) try:
encoder_x = (int)(socket.SocketIO(self.s).readline())
except:
pass
encoder_y = (int)(socket.SocketIO(self.s).readline())
encoder_z = (int)(socket.SocketIO(self.s).readline())
last_x, last_y, last_z = gtm.current_steps last_x, last_y, last_z = gtm.current_steps
# We are now at some position which realizes the encoder positions # We are now at some position which realizes the encoder positions
# curr_x = encoder_x + k*X_MOTOR_STEPS # curr_x = encoder_x + k*X_MOTOR_STEPS
# |curr_x - last_x| <= 1/2 * X_MOTOR_STEPS # |curr_x - last_x| <= 1/2 * X_MOTOR_STEPS
curr_x = encoder_x + (last_x // gtm.X_MOTOR_STEPS) * gtm.X_MOTOR_STEPS curr_x = encoder_x + (last_x // gtm.X_MOTOR_STEPS) * gtm.X_MOTOR_STEPS
if(curr_x - last_x > .5 * gtm.X_MOTOR_STEPS): if curr_x - last_x > .5 * gtm.X_MOTOR_STEPS:
curr_x -= gtm.X_MOTOR_STEPS curr_x -= gtm.X_MOTOR_STEPS
curr_y = encoder_y + (last_y // gtm.Y_MOTOR_STEPS) * gtm.Y_MOTOR_STEPS curr_y = encoder_y + (last_y // gtm.Y_MOTOR_STEPS) * gtm.Y_MOTOR_STEPS
if(curr_y - last_y > .5 * gtm.Y_MOTOR_STEPS): if curr_y - last_y > .5 * gtm.Y_MOTOR_STEPS:
curr_y -= gtm.Y_MOTOR_STEPS curr_y -= gtm.Y_MOTOR_STEPS
curr_z = encoder_z + (last_z // gtm.Z_MOTOR_STEPS) * gtm.Z_MOTOR_STEPS curr_z = encoder_z + (last_z // gtm.Z_MOTOR_STEPS) * gtm.Z_MOTOR_STEPS
if(curr_z - last_z > .5 * gtm.Z_MOTOR_STEPS): if curr_z - last_z > .5 * gtm.Z_MOTOR_STEPS:
curr_z -= gtm.Z_MOTOR_STEPS curr_z -= gtm.Z_MOTOR_STEPS
# Note that if we don't probe regularly enough, we lose track of the position # Note that if we don't probe regularly enough, we lose track of the position
# Really, we should be able to set_steps # Really, we should be able to set_steps
self.set_position([curr_x/gtm.X_STEPS_PER_MM, gtm.set_position([curr_x / gtm.X_STEPS_PER_MM,
curr_y / gtm.Y_STEPS_PER_MM, curr_y / gtm.Y_STEPS_PER_MM,
curr_z / gtm.Z_STEPS_PER_MM]) curr_z / gtm.Z_STEPS_PER_MM])
def realize(self, gtm): def realize(self, gtm):
f.write("realize") self.s.send("realize")
if(gtm.throttle_x == 0): if gtm.throttle_x == 0:
f.write(gtm.x_direction + " 0") self.s.send(gtm.x_direction + " 0")
else: else:
f.write(gtm.x_direction + " " + gtm.FAST_XY_FEEDRATE/gtm.x_throttle) self.s.send(gtm.x_direction + " " + gtm.FAST_XY_FEEDRATE / gtm.x_throttle)
if(gtm.throttle_y == 0): if gtm.throttle_y == 0:
f.write(gtm.y_direction + " 0") self.s.send(gtm.y_direction + " 0")
else: else:
f.write(gtm.y_direction + " " + gtm.FAST_XY_FEEDRATE/gtm.y_throttle) self.s.send(gtm.y_direction + " " + gtm.FAST_XY_FEEDRATE / gtm.y_throttle)
if(gtm.throttle_z == 0): if gtm.throttle_z == 0:
f.write(gtm.z_direction + " 0") self.s.send(gtm.z_direction + " 0")
else: else:
f.write(gtm.z_direction + " " + gtm.FAST_Z_FEEDRATE/gtm.z_throttle) self.s.send(gtm.z_direction + " " + gtm.FAST_Z_FEEDRATE / gtm.z_throttle)

8
simulator.py Normal file
View file

@ -0,0 +1,8 @@
import hardware
class Simulator:
def __init__(self, port):
self.socket = port