#!/usr/bin/env python
#-*- coding: utf-8 -*-

import thread, time
import numpy as np
from gpiozero import Motor, PWMLED

# Eyelib can be managed by following variables:
#    blinking : If True start a blink animation (1 cycle)
#    blinkspd : Speed of blinking
#    eye : Eye image
#    lid : Lid base image
import eyelib

# Last dewarped frame can be accessed by using variable "panorama"
# A copy should be used to access data in order to avoid in-between garbage
import dewarp

# Constants
M_FWD = 0 # GPIO pin for forward movement
M_BWD = 0 # GPIO pin for backward movement
M_CKW = 0 # GPIO pin for clockwise rotation
M_CCW = 0 # GPIO pin for counterclockwise rotation
BLIGHT = 0 # GPIO pin for TFT backlight control
ANG_SPD = 0 # Angular speed for head rotation
Facing = 0 # Current direction (approximate)

# Actuators creation
motor = Motor(M_FWD, M_BWD, pwm=True)
head = Motor(M_CKW, M_CCW, pwm=True)
backlight = PWMLED(BLIGHT)

# Move the spider forward or backward. Speed -1..0 = backward, 0..1 = forward
def Move(spd):
    if (spd>0):
        motor.forward(spd)
    else:
        motor.backward(abs(spd))
    if (spd==0):
        motor.stop()
        
# Rotate the head. Angle is the approximate rotation (0..360). Speed -1..0 = counterclockwise, 0..1 = clockwise        
def Rotate(angle,spd):   
    global Facing
    if (spd>0):
        head.forward(spd)
    else:
        head.backward(abs(spd))
    if (spd<>0):    
        time.sleep(ANG_SPD(angle/abs(spd)))
    head.stop()    
    Facing=(Facing-(angle*(np.sign(spd))))%360

# Main program
if __name__ == '__main__':
    backlight.value=1 # Start with backlight at full brightness
    motor.stop() # Be sure the robot is not moving
    head.stop() # Be sure the robot is not moving
    
    thread.start_new_thread(Eye, ()) # Eye thread
    thread.start_new_thread(UnWarp, ()) # Unwarping thread
    
    while True: # Loop forever
        pass

@@ -58,16 +58,21 @@
 ad, time
+, cv2
 %0Aimport 
@@ -1540,16 +1540,549 @@
 ))))%25360
+%0A    %0A# Returns the angles of the brightest spot in the panoramic frame (x = horizontal, y = vertical)%0Adef FindBrightestSpot(img):%0A    gray = cv2.cvtColor(dewarp.panorama, cv2.COLOR_BGR2GRAY)%0A    gray = cv2.GaussianBlur(gray, (11, 11), 0)%0A    minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(gray)%0A    X = ((dewarp.Wd-maxLoc%5B0%5D)*360)/dewarp.Wd # Offset to be added to fix start position of the panorama%0A    Y = ((dewarp.Hd-maxLoc%5B0%5D)*30)/dewarp.Hd # Should be fixed after computing the right vertical FOV of the panorama%0A    return X,Y
 %0A%0A# Main

Update spider.py