Skip to content
Snippets Groups Projects
Commit 50068371 authored by Aaron John Sabu's avatar Aaron John Sabu
Browse files

Merge branch 'reconnect' into espcam-reconnect

parents 7330c534 9dc2f195
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
from websocket import create_connection
import pygame
import time
def direction_translation(dir1, dir2, dir3, dir4):
new_dir1 = 0
new_dir2 = 0
new_dir3 = 0
new_dir4 = 0
if dir1 == '+':
new_dir1 = 2
else:
......@@ -32,15 +30,44 @@ def direction_translation(dir1, dir2, dir3, dir4):
spinning = ""
vertical = ""
return new_dir1, new_dir2, new_dir3, new_dir4
class Blimp:
def __init__(self):
print("Address of self = ",id(self))
target = "ws://10.0.0.2:81"
self.ws = create_connection(target)
print("connected")
print("Address of Blimp object: ", id(self))
self.target = "ws://10.0.0.2:81"
self.ESP_RECONNECT_MAXTRIES = 10
self.connectionStatus = False
try:
self.connectionStatus = True
self.ws = create_connection(self.target)
except:
self.connectionStatus = False
self.try_reconnect()
print("Connected")
def try_reconnect(self):
reconnect_try = 1;
self.connectionStatus = False
while not self.connectionStatus:
if (reconnect_try > self.ESP_RECONNECT_MAXTRIES):
print("Reconnect not possible: Giving up")
exit()
print("Trying to reconnect");
try:
self.ws.close()
except:
pass
try:
self.ws = create_connection(self.target)
self.connectionStatus = True
return True
except:
self.connectionStatus = False
reconnect_try += 1
time.sleep(0.2)
self.ws = create_connection(self.target)
return True
def get_LidarDist1(self):
return 0
......@@ -98,7 +125,7 @@ class Blimp:
spin = "TURNING LEFT"
if (pwm3 > pwm4):
thrust = "BACKWARD"
elif (pwm3 == pwm4):
elif (pwm3 == pwm4):
thrust = "NONE"
else:
thrust = "FORWARD"
......@@ -116,9 +143,7 @@ class Blimp:
try:
self.ws.send_binary(bytes(cmd))
except Exception as e:
target = "ws://10.0.0.2:81"
self.ws.close()
self.ws = create_connection(target)
print("connected")
self.try_reconnect()
self.ws.send_binary(bytes(cmd))
pygame.time.wait(20)
......@@ -802,4 +802,4 @@ if __name__ == '__main__':
print_count = 0
pygame.time.wait(50)
pygame.quit()
pygame.quit()
\ No newline at end of file
No preview for this file type
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment