diff options
Diffstat (limited to 'nicla')
-rw-r--r-- | nicla/consts.py | 6 | ||||
-rw-r--r-- | nicla/road.py | 56 | ||||
-rw-r--r-- | nicla/uart.py (renamed from nicla/serial_test.py) | 0 |
3 files changed, 62 insertions, 0 deletions
diff --git a/nicla/consts.py b/nicla/consts.py new file mode 100644 index 0000000..b5ccd9a --- /dev/null +++ b/nicla/consts.py @@ -0,0 +1,6 @@ +DUI_CMD_SIGN_START = 0x01 +DUI_CMD_SIGN_END = 0x0f +DUI_CMD_SPEED_START = 0x10 +DUI_CMD_SPEED_END = 0x1f +DUI_CMD_STEER_START = 0x20 +DUI_CMD_STEER_END = 0xff diff --git a/nicla/road.py b/nicla/road.py new file mode 100644 index 0000000..c5ffae3 --- /dev/null +++ b/nicla/road.py @@ -0,0 +1,56 @@ +import sensor, image, time, math +import uart +from consts import * + +sensor.reset() +sensor.set_pixformat(sensor.RGB565) +sensor.set_framesize(sensor.HVGA) +sensor.skip_frames(time = 2000) +clock = time.clock() + +WIDTH = 480 +HEIGHT = 320 + +HORIZON = 120 +STRETCH = 155 +SQUEEZE = 400 + +STEERING_ENTHOUSIASM = 7.0 +ROAD_MIN_BRIGHTNESS = 0xa0 + +points = [(STRETCH, HORIZON), + (WIDTH-1-STRETCH, HORIZON), + (WIDTH-1+SQUEEZE, HEIGHT-1), + (-SQUEEZE, HEIGHT-1)] + +def main(): + img = sensor.snapshot() + 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 = blob.area() + 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): + main() + uart.uart_buffer(DUI_CMD_SPEED_END) + clock.tick() diff --git a/nicla/serial_test.py b/nicla/uart.py index 276f6d1..276f6d1 100644 --- a/nicla/serial_test.py +++ b/nicla/uart.py |