Skip to content

Commit

Permalink
Code used in project demonstrations
Browse files Browse the repository at this point in the history
  • Loading branch information
AparajitoSaha committed May 21, 2021
1 parent fda5ded commit 6ff9a57
Show file tree
Hide file tree
Showing 15 changed files with 564 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# ExPiRior
The entire codebase for the ExPiRior autonomous coronavirus testing site
The entire codebase for the ExPiRior autonomous coronavirus testing site. Maintained by Aparajito Saha and Amulya Khurana, School of Electrical and Computer Engineering, Cornell University.
102 changes: 102 additions & 0 deletions arm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import RPi.GPIO as GPIO
import time
from gpiozero import Servo

def move_arm():
claw = Servo(19,0)
updown = 12
elbow = Servo(13)

rotate = 21
GPIO.setmode(GPIO.BCM)

GPIO.setup(rotate, GPIO.OUT)
GPIO.setup(updown, GPIO.OUT)
r = GPIO.PWM(rotate, 50)
p = GPIO.PWM(updown, 50)

#p.start(9.5)
r.start(2.5)

claw.mid()
r.ChangeDutyCycle(2.5)
print("Moved towards tubes")
time.sleep(2)
print("Grabbing tube")
time.sleep(2)
claw.max()
time.sleep(5)

print("Moving to user")
r.ChangeDutyCycle(12.5)
time.sleep(2)
print("Closing claw")
time.sleep(2)

#GPIO.cleanup()
#claw.stop()

def calibrate_updown():
p.ChangeDutyCycle(7.5)

def calibrate_rotate():
r.ChangeDutyCycle(2.5)

def calibrate():
r.ChangeDutyCycle(7.5)
claw.max()
elbow.mid()
p.ChangeDutyCycle(22.5)
print("Calibrating - everything in mid position, claw closed")
time.sleep(2)

def grab_tube():
claw.mid()
r.ChangeDutyCycle(2.5)
print("Moved towards tubes")
time.sleep(2)
print("Grabbing tube")
time.sleep(2)
claw.max()
time.sleep(2)

def grab_swab():
r.ChangeDutyCycle(3.5)
claw.max()
elbow.mid()
print("Moved towards tubes")
time.sleep(2)
print("Grabbing tube")
p.ChangeDutyCycle(22.5)
claw.value = 0.65
claw.mid()
time.sleep(3)
claw.max()
time.sleep(1)
print("Moving up")
#p.ChangeDutyCycle(27.5)
time.sleep(2)

def release():
print("Moving to user")
r.ChangeDutyCycle(12.5)
time.sleep(2)
print("Closing claw")
time.sleep(2)

#calibrate()
#time.sleep(5)
#grab_tube()
#claw.max()
#claw.mid()
#time.sleep(3)
#claw.max()
#release()
#calibrate_updown()
#time.sleep(2)
#calibrate_rotate()
#time.sleep(2)
#p.stop()
#r.stop()
#GPIO.cleanup()
#claw.stop()
98 changes: 98 additions & 0 deletions client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from threading import Thread
import socket, time
from print_manipulation import *
import csv

IP_ADDRESS = '192.168.137.216'
IP_PORT = 65432

class Client():
def run(self):
print("Client started")
while True:
try:
rcvMsg = self.readServerMsg()
except:
print("Exception in Client.run()")
isClientRunning = False
closeConnection()
break
print("Client thread terminated")

def readServerMsg(self):
bufSize = 2048
data = ""
while data[-1:] != "\0": # reply with end-of-message indicator
try:
blk = sock.recv(bufSize)
except:
raise Exception("Exception from blocking sock.recv()")
data += blk.decode('utf-8')
print("Data received:", data)
return data

def sendCommand(data):
print("sendCommand() with msg = " + data)
data = data + "\0"
data = bytes(data, 'utf-8')
try:
# append \0 as end-of-message indicator
sock.sendall(data)
except:
print("Exception in sendCommand()")
closeConnection()

def closeConnection():
global isConnected
print("Closing socket")
sock.close()
isConnected = False

def connect():
global sock
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Connecting...")
try:
sock.connect((IP_ADDRESS, IP_PORT))
except:
print("Connection failed.")
return False
return True

def read_csv(id):
rows = []
data = open("sample_student_data_read.csv", "r")
csvreader = csv.reader(data)
# extracting each data row one by one
for row in csvreader:
rows.append(row)
for row in rows:
if row[0] == str(id):
return row[2] + " " + row[3]

return ""

sock = None
isConnected = False
id = 0

if connect():
isConnected = True
client = Client()
print("Connection established")
time.sleep(1)
while isConnected:
rcvd = client.readServerMsg()
if (rcvd[:-1] == "yes"):
print_label(id)
elif (rcvd[0] == '@'):
id = rcvd[1:-1]
print(id, len(id))
id = int(id)
print(id, type(id))
data = read_csv(id)
if (type(id) == int):
sendCommand(data)
else:
print("Connection to %s:%d failed" % (IP_ADDRESS, IP_PORT))
print("done")
11 changes: 11 additions & 0 deletions client_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import socket

HOST = '192.168.0.241' # The server's hostname or IP address
PORT = 65432 # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)

print('Received', repr(data))
38 changes: 38 additions & 0 deletions print_manipulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import time
import pyautogui
import csv

def print_label(id):
fields = []
rows = []

data = open("sample_student_data_read.csv", "r")
csvreader = csv.reader(data)
# extracting field names through first row
fields = next(csvreader)

# extracting each data row one by one
for row in csvreader:
rows.append(row)

ind = 0
time.sleep(5)

for row in rows:
if row[0] != str(id):
pyautogui.press('down')
ind = rows.index(row)
ind += 1
else:
for col in row:
print(col)
break

time.sleep(5)
pyautogui.hotkey('ctrl', 'p')
time.sleep(3)
pyautogui.hotkey('enter')
time.sleep(3)

for i in range(ind):
pyautogui.press('up')
2 changes: 2 additions & 0 deletions record.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
sudo arecord --format=S16_LE --rate=44100 -D hw:1,0 --file-type=wav hi.wav
2 changes: 2 additions & 0 deletions redo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
sudo arecord --format=S16_LE --rate=44100 -D hw:1,0 --file-type=wav redo.wav
6 changes: 6 additions & 0 deletions sample_student_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
StudentId,Name,NetID,Date of Birth
4875937,Aparajito Saha,as2537,07-07-2000
1234567,Jonathan Doe,jd098,01-01-1999
9876543,Janet Smith,js100,10/21/2002
1357924,Joseph Skovira,jfs9,06/22/1998
2833794,Amulya Khurana,ak2425,3/30/2000
6 changes: 6 additions & 0 deletions sample_student_data_read.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
StudentId,Name,NetID,Date of Birth
4873837,Aparajito Saha,as2537,07-07-2000
1234567,Jonathan Doe,jd098,01-01-1999
9876543,Janet Smith,js100,10/21/2002
1357924,Joseph Skovira,jfs9,06/22/1990
2930848,Amulya Khurana,ak2425,03/30/2000
45 changes: 45 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import socket
import threading as Thread
import time
import sys

class SocketServer():
def __init__(self, conn):
# conn represents client connection, initializes the TCP/IP connection
# between server on Pi and client on laptop
self.conn = conn

def run(self):
# Runs the server on the Pi, checking for incoming connections from
# client over wireless TCP/IP
global isConnected
print("Python server started")
while True:
cmd = ""
try:
print("Calling blocking conn.recv()")
msg = recvClientMessage()
except:
print("exception in conn.recv()")
# happens when connection is reset from the client
break

conn.close()
print("Client disconnected. Waiting for next client...")
isConnected = False
print("SocketServer terminated")

def sendClientMessage(self, msg):
# Sends a string message over to the client
msg = msg + "\0"
msg = bytes(msg, 'utf-8')
print("Message sent to client: ", msg)
self.conn.sendall(msg)

def recvClientMessage(self):
# receives message from client and passes it on to
# other code modules
msg = self.conn.recv(4096)
msg = msg.decode('utf-8')
print("Message received = ", msg[:-1])
return msg
Binary file added server_print_layout.lbx
Binary file not shown.
19 changes: 19 additions & 0 deletions server_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import socket

HOST = '192.168.0.201' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
print("binding socket")
s.bind((HOST, PORT))
print("socket ready")
s.listen()
print("waiting for connection")
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
Loading

0 comments on commit 6ff9a57

Please sign in to comment.