Skip to content

Commit

Permalink
black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
misko committed Nov 27, 2023
1 parent 3827aff commit ec3d099
Show file tree
Hide file tree
Showing 32 changed files with 6,270 additions and 4,703 deletions.
348 changes: 187 additions & 161 deletions software/grbl/grbl_interactive.py

Large diffs are not rendered by default.

38 changes: 21 additions & 17 deletions software/grbl/mm_per_step.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
teeth_per_revolution=20
spacing_per_tooth=2 # 2mm
gear_ratio=26+103.0/121
degrees_per_step_wo_gear=1.8
teeth_per_revolution = 20
spacing_per_tooth = 2 # 2mm
gear_ratio = 26 + 103.0 / 121
degrees_per_step_wo_gear = 1.8

degrees_per_step=(degrees_per_step_wo_gear/gear_ratio)
steps_per_revolution=360/degrees_per_step
micro_stepping=16
steps_per_mm=micro_stepping*steps_per_revolution/(teeth_per_revolution*spacing_per_tooth)
degrees_per_step = degrees_per_step_wo_gear / gear_ratio
steps_per_revolution = 360 / degrees_per_step
micro_stepping = 16
steps_per_mm = (
micro_stepping * steps_per_revolution / (teeth_per_revolution * spacing_per_tooth)
)


# $100=2148.000
# $101=2148.000
# $102=800.000
# $103=800.000
# $110=700.000
# $111=700.000

#$100=2148.000
#$101=2148.000
#$102=800.000
#$103=800.000
#$110=700.000
#$111=700.000

print('$100=%d\n\
print(
"$100=%d\n\
$101=%d\n\
$110=1000\n\
$111=1000\n\
$120=50\n\
$121=50\n' % (int(steps_per_mm),int(steps_per_mm)))
$121=50\n"
% (int(steps_per_mm), int(steps_per_mm))
)
46 changes: 24 additions & 22 deletions software/grbl/run_grbl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,49 @@
import time
import sys

if len(sys.argv)!=3:
print("%s device file" % sys.argv[0])
sys.exit(1)
if len(sys.argv) != 3:
print("%s device file" % sys.argv[0])
sys.exit(1)

serial_fn,gcode_fn=sys.argv[1:]
serial_fn, gcode_fn = sys.argv[1:]


# Open grbl serial port ==> CHANGE THIS BELOW TO MATCH YOUR USB LOCATION
s = serial.Serial(serial_fn,115200) # GRBL operates at 115200 baud. Leave that part alone.
s = serial.Serial(
serial_fn, 115200
) # GRBL operates at 115200 baud. Leave that part alone.

# Open g-code file
f = open(gcode_fn,'r');
f = open(gcode_fn, "r")

# Wake up grbl
s.write("?".encode())
print(s.readline().strip())
s.write(b'\x18')
s.write(b"\x18")
print(s.readline().strip())
s.write("?".encode())
print(s.readline().strip())
s.write("?".encode())
print(s.readline().strip())
#s.write("G0 X100\n".encode())
#print(s.readline().strip())
# s.write("G0 X100\n".encode())
# print(s.readline().strip())

#sys.exit(1)
#time.sleep(2) # Wait for grbl to initialize
#s.flushInput() # Flush startup text in serial input
# sys.exit(1)
# time.sleep(2) # Wait for grbl to initialize
# s.flushInput() # Flush startup text in serial input

# Stream g-code to grbl
for line in f:
l = line.strip() # Strip all EOL characters for consistency
if l[0]==';':
continue
print('Sending: ' + l)
s.write((l + '\n').encode()) # Send g-code block to grbl
grbl_out = s.readline() # Wait for grbl response with carriage return
print(grbl_out)

# Wait here until grbl is finished to close serial port and file.
#raw_input(" Press <Enter> to exit and disable grbl.")
l = line.strip() # Strip all EOL characters for consistency
if l[0] == ";":
continue
print("Sending: " + l)
s.write((l + "\n").encode()) # Send g-code block to grbl
grbl_out = s.readline() # Wait for grbl response with carriage return
print(grbl_out)

# Wait here until grbl is finished to close serial port and file.
# raw_input(" Press <Enter> to exit and disable grbl.")

# Close file and serial port
f.close()
Expand Down
173 changes: 104 additions & 69 deletions software/grbl_sdr_collect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sdrpluto.gather import *
from grbl.grbl_interactive import GRBLManager
from sdrpluto.gather import *
from grbl.grbl_interactive import GRBLManager
from model_training_and_inference.utils.rf import beamformer
import threading
import time
Expand All @@ -8,106 +8,141 @@
import os
import pickle


def bounce_grbl(gm):
direction=None
direction = None
while gm.collect:
print("TRY TO BOUNCE")
try:
direction=gm.bounce(1,direction=direction)
direction = gm.bounce(1, direction=direction)
except Exception as e:
print(e)
print("TRY TO BOUNCE RET")
time.sleep(10) # cool off the motor
time.sleep(10) # cool off the motor


if __name__=='__main__':
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--receiver-ip", type=str, help="receiver Pluto IP address",required=True)
parser.add_argument("--emitter-ip", type=str, help="emitter Pluto IP address",required=True)
parser.add_argument("--fi", type=int, help="Intermediate frequency",required=False,default=1e5)
parser.add_argument("--fc", type=int, help="Carrier frequency",required=False,default=2.5e9)
parser.add_argument("--fs", type=int, help="Sampling frequency",required=False,default=16e6)
parser.add_argument("--cal0", type=int, help="Rx0 calibration phase offset in degrees",required=False,default=180)
#parser.add_argument("--d", type=int, help="Distance apart",required=False,default=0.062)
parser.add_argument("--rx-gain", type=int, help="RX gain",required=False,default=-3)
parser.add_argument("--tx-gain", type=int, help="TX gain",required=False,default=-3)
parser.add_argument("--grbl-serial", type=str, help="serial file for GRBL",required=True)
parser.add_argument("--out", type=str, help="output file",required=True)
parser.add_argument("--record-freq", type=int, help="record freq",required=False,default=5)
parser.add_argument("--rx-mode", type=str, help="rx mode",required=False,default="fast_attack")
parser.add_argument("--record-n", type=int, help="records",required=False,default=43200)
parser.add_argument("--rx-n", type=int, help="RX buffer size",required=False,default=2**12)
parser.add_argument(
"--receiver-ip", type=str, help="receiver Pluto IP address", required=True
)
parser.add_argument(
"--emitter-ip", type=str, help="emitter Pluto IP address", required=True
)
parser.add_argument(
"--fi", type=int, help="Intermediate frequency", required=False, default=1e5
)
parser.add_argument(
"--fc", type=int, help="Carrier frequency", required=False, default=2.5e9
)
parser.add_argument(
"--fs", type=int, help="Sampling frequency", required=False, default=16e6
)
parser.add_argument(
"--cal0",
type=int,
help="Rx0 calibration phase offset in degrees",
required=False,
default=180,
)
# parser.add_argument("--d", type=int, help="Distance apart",required=False,default=0.062)
parser.add_argument(
"--rx-gain", type=int, help="RX gain", required=False, default=-3
)
parser.add_argument(
"--tx-gain", type=int, help="TX gain", required=False, default=-3
)
parser.add_argument(
"--grbl-serial", type=str, help="serial file for GRBL", required=True
)
parser.add_argument("--out", type=str, help="output file", required=True)
parser.add_argument(
"--record-freq", type=int, help="record freq", required=False, default=5
)
parser.add_argument(
"--rx-mode", type=str, help="rx mode", required=False, default="fast_attack"
)
parser.add_argument(
"--record-n", type=int, help="records", required=False, default=43200
)
parser.add_argument(
"--rx-n", type=int, help="RX buffer size", required=False, default=2**12
)
args = parser.parse_args()

#setup output recorder
record_matrix = np.memmap(args.out, dtype='float32', mode='w+', shape=(args.record_n,5+65))
# setup output recorder
record_matrix = np.memmap(
args.out, dtype="float32", mode="w+", shape=(args.record_n, 5 + 65)
)

#setup GRBL
gm=GRBLManager(args.grbl_serial)
# setup GRBL
gm = GRBLManager(args.grbl_serial)

#calibrate the receiver
sdr_rx=setup_rxtx_and_phase_calibration(args)
if sdr_rx==None:
# calibrate the receiver
sdr_rx = setup_rxtx_and_phase_calibration(args)
if sdr_rx == None:
print("Failed phase calibration, exiting")
sys.exit(1)
phase_calibration=sdr_rx.phase_calibration
sdr_rx=None
sdr_rx,sdr_tx=setup_rx_and_tx(args)
if sdr_rx==None or sdr_tx==None:
phase_calibration = sdr_rx.phase_calibration
sdr_rx = None
sdr_rx, sdr_tx = setup_rx_and_tx(args)
if sdr_rx == None or sdr_tx == None:
print("Failed setup, exiting")
sys.exit(1)

#apply the previous calibration
sdr_rx.phase_calibration=phase_calibration
# apply the previous calibration
sdr_rx.phase_calibration = phase_calibration

#start moving GRBL
# start moving GRBL
gm_thread = threading.Thread(target=bounce_grbl, args=(gm,))
gm_thread.start()

pos=np.array([[-0.03,0],[0.03,0]])
pos = np.array([[-0.03, 0], [0.03, 0]])

time_offset=time.time()
time_offset = time.time()
for idx in range(args.record_n):
while not gm.position['is_moving']:
while not gm.position["is_moving"]:
print("wait for movement")
time.sleep(1)

#get some data
# get some data
try:
signal_matrix=sdr_rx.rx()
signal_matrix = sdr_rx.rx()
except Exception as e:
print("Failed to receive RX data! removing file",e)
print("Failed to receive RX data! removing file", e)
os.remove(args.out)
break
signal_matrix[1]*=np.exp(1j*sdr_rx.phase_calibration)
current_time=time.time()-time_offset # timestamp
signal_matrix[1] *= np.exp(1j * sdr_rx.phase_calibration)
current_time = time.time() - time_offset # timestamp

beam_thetas,beam_sds,beam_steer=beamformer(
pos,
signal_matrix,
args.fc
)
beam_thetas, beam_sds, beam_steer = beamformer(pos, signal_matrix, args.fc)

avg_phase_diff=get_avg_phase(signal_matrix)
xy=gm.position['xy']
record_matrix[idx]=np.hstack(
[
np.array([current_time,xy[0],xy[1],avg_phase_diff[0],avg_phase_diff[1]]),
beam_sds])
#time.sleep(1.0/args.record_freq)
avg_phase_diff = get_avg_phase(signal_matrix)
xy = gm.position["xy"]
record_matrix[idx] = np.hstack(
[
np.array(
[current_time, xy[0], xy[1], avg_phase_diff[0], avg_phase_diff[1]]
),
beam_sds,
]
)
# time.sleep(1.0/args.record_freq)

if idx%50==0:
elapsed=time.time()-time_offset
rate=elapsed/(idx+1)
print(idx,
"mean: %0.4f" % avg_phase_diff[0],
"_mean: %0.4f" % avg_phase_diff[1],
"%0.4f" % (100.0*float(idx)/args.record_n),
'%',
"elapsed(min): %0.1f" % (elapsed/60),
"rate(s/idx): %0.3f" % rate,
"remaining(min): %0.3f" % ((args.record_n-idx)*rate/60))
gm.collect=False
if idx % 50 == 0:
elapsed = time.time() - time_offset
rate = elapsed / (idx + 1)
print(
idx,
"mean: %0.4f" % avg_phase_diff[0],
"_mean: %0.4f" % avg_phase_diff[1],
"%0.4f" % (100.0 * float(idx) / args.record_n),
"%",
"elapsed(min): %0.1f" % (elapsed / 60),
"rate(s/idx): %0.3f" % rate,
"remaining(min): %0.3f" % ((args.record_n - idx) * rate / 60),
)
gm.collect = False
print("Done collecting!")
gm_thread.join()
#plot_recv_signal(sdr_rx)

# plot_recv_signal(sdr_rx)
Loading

0 comments on commit ec3d099

Please sign in to comment.