Skip to content

Commit

Permalink
Partial integration with multi-threading
Browse files Browse the repository at this point in the history
  • Loading branch information
harjaus committed Jun 18, 2019
1 parent 2875a69 commit 468e0f0
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions zedMain.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,35 @@
startFrom = 270
global steeringFactor
steeringFactor = 30
global carVelocity
carVelocity = 60



def issueCommands(steering, velocity, exit, lastCommandTime=0.025):
if 'car' not in issueCommands.__dict__:
if 'car' not in issueCommands.__dict__: #only runs once
issueCommands.car = fspycan_ext.Car("can0")
issueCommands.car.init()
issueCommands.setup = True
print("Initiating CAN setup.")

while issueCommands.setup: #can setup protocol
issueCommands.setup = issueCommands.car.setupCAN()
time.sleep(0.006) # >=5ms, <50ms
#input("finished setup")
issueCommands.car.set_steering_velocity(int(steering), int(velocity))
issueCommands.car.commandsLoop()
issueCommands.car.setupCAN() #function runs until we finish setup
print("Setup finished gracefully")

if lastCommandTime < 0.005:
print("Sending commands too fast. Waiting 10ms")
time.sleep(0.01)
elif lastCommandTime > 0.05:
print("Sent command too late.\n Stop the program.")
#issueCommands.setup = True
issueCommands.car.set_steering_velocity(int(steering), int(velocity))
#we only set the steering here, the loop runs on a different c++ thread

if exit: #can exit protocol
print("Initiating CAN exit.")
while issueCommands.car.exitCAN():
time.sleep(0.025)
issueCommands.car.exitCAN() #runs until we exit gracefully

def calculateCenter(target):

newCom = target - int(width/2) #offset from center of image

if newCom != -int(width/2): #when we have a correct reading

final = calculateCenter.pastCom + (newCom-calculateCenter.pastCom) / 2
calculateCenter.pastCom = final

return round(final)
calculateCenter.pastCom = 0

def imCapt(zed):
"""Used for parallelised image and depth campturing."""
Expand Down Expand Up @@ -119,14 +111,17 @@ def main(visual=False, green=False) :

# Create a ZED camera object
zed = sl.Camera()
lastCommand = 0

# Set configuration parameters
init = sl.InitParameters()
init.camera_resolution = sl.RESOLUTION.RESOLUTION_HD720
init.camera_fps = 60 # Set max fps at 100

init.depth_mode = sl.DEPTH_MODE.DEPTH_MODE_ULTRA
init.coordinate_units = sl.UNIT.UNIT_METER

calculateCenter.pastCom = 0
lastCommand = 0

if len(sys.argv) > 1 :
visual = sys.argv[1]
Expand Down Expand Up @@ -169,9 +164,9 @@ def main(visual=False, green=False) :

if reading:
print("Camera: ", reading)
issueCommands((reading/steeringFactor)*-1, 60, False, time.time()-lastCommand)
issueCommands((reading/steeringFactor)*-1, carVelocity, False, time.time()-lastCommand)
else:
issueCommands((calculateCenter.pastCom/steeringFactor)*-1, 60, False, time.time()-lastCommand)
issueCommands((calculateCenter.pastCom/steeringFactor)*-1, carVelocity, False, time.time()-lastCommand)
lastCommand = time.time()
t.join()
#print("Frames left: ", framesToDo-amount)
Expand All @@ -183,6 +178,7 @@ def main(visual=False, green=False) :
except KeyboardInterrupt:
issueCommands(0,0,True) #initiates the exit protocol
zed.close()
quit()

issueCommands(0,0,True)
zed.close()
Expand Down

0 comments on commit 468e0f0

Please sign in to comment.