aboutsummaryrefslogtreecommitdiff
path: root/nicla/road.py
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-06-08 09:33:12 +0200
committerlonkaars <loek@pipeframe.xyz>2023-06-08 09:33:12 +0200
commitc81c6223b7d9e5973f5d2825c399d5777e093c58 (patch)
treea7eceaf2e0b6525f6aeacaedb1b33e42582529e8 /nicla/road.py
parent97939ee4eaad5937a0a2eee190b2b9028d009a97 (diff)
parent2903e61cbe2eeff3121f67da516ea195999f0bba (diff)
merge dev into master
Diffstat (limited to 'nicla/road.py')
-rw-r--r--nicla/road.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/nicla/road.py b/nicla/road.py
new file mode 100644
index 0000000..143a9c5
--- /dev/null
+++ b/nicla/road.py
@@ -0,0 +1,73 @@
+import sensor, image, time, math
+import uart
+import signs_detect
+import traffic_light
+from consts import *
+
+sensor.reset()
+sensor.set_pixformat(sensor.RGB565)
+sensor.set_framesize(sensor.HVGA)
+sensor.skip_frames(time = 4000)
+clock = time.clock()
+
+WIDTH = 480
+HEIGHT = 320
+MAX_AREA = WIDTH * HEIGHT / 10
+MIN_AREA = 40
+
+HORIZON = 150
+STRETCH = 40
+SQUEEZE = 400
+
+STEERING_ENTHOUSIASM = 3.0
+ROAD_MIN_BRIGHTNESS = 0xa0
+
+points = [(STRETCH, HORIZON),
+ (WIDTH-1-STRETCH, HORIZON),
+ (WIDTH-1+SQUEEZE, HEIGHT-1),
+ (-SQUEEZE, HEIGHT-1)]
+
+
+def drive(img):
+ img.to_grayscale()
+ img.replace(vflip=True, hmirror=True)
+ img.rotation_corr(corners=points)
+ img.gaussian(3)
+
+ offset_sum = 0.0
+ offset_count = 0.0
+ for blob in img.find_blobs([(ROAD_MIN_BRIGHTNESS, 0xff)], pixels_threshold=100):
+ img.draw_rectangle(blob.rect())
+ area_weight = MIN_AREA + min(MAX_AREA, blob.w() * blob.h()) # limit max area_weight so small blobs still have impact
+ horizontal_pos = (blob.x() + blob.w()/2) / WIDTH
+ offset_sum += horizontal_pos * area_weight
+ offset_count += area_weight
+ # dit tegen niemand zeggen
+ if offset_count < 0.01: return
+ avg = offset_sum / offset_count
+ avg = avg * 2 - 1
+ avg *= STEERING_ENTHOUSIASM
+ avg = max(-1, min(1, avg))
+
+ steerByte = int((avg + 1.0) * (DUI_CMD_STEER_END - DUI_CMD_STEER_START) / 2 + DUI_CMD_STEER_START)
+
+ uart.uart_buffer(steerByte)
+
+
+
+while(True):
+
+ #img = sensor.snapshot()
+ #data = traffic_light.traf_lights(img)
+ #if data is not None:
+ #uart.uart_buffer(data)
+
+
+ sign_img = sensor.snapshot()
+ data_sign = signs_detect.sign_detection(sign_img)
+ if data_sign is not None:
+ uart.uart_buffer(data_sign)
+
+ drive_img = sensor.snapshot()
+ drive(drive_img)
+ uart.uart_buffer(0x1f)