More Sensitive

This commit is contained in:
Steven Tracey 2023-04-03 14:00:21 -04:00
parent 9836e6058f
commit e9588703d0
5 changed files with 122 additions and 6 deletions

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (pythonProject)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (SunFollower)" project-jdk-type="Python SDK" />
</project>

View File

@ -12,7 +12,7 @@
<excludeFolder url="file://$MODULE_DIR$/.idea" />
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="Python 3.9 (SunFollower)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="MicroPython" level="project" />
</component>

View File

@ -1,2 +1,15 @@
# SunFollower
## How to Upload to Pi
1. Go to the [Thonny](https://thonny.org/) website
2. Hover over "Windows" option in the Download Options box
3. Click the download for "Installer with 64-bit Python 3.10"
4. Run the installer when the download finishes
5. Select "Install for me only"
6. Click "Next" through all the options
7. Click "Install", then "Finish"
8. Run Thonny (Click "Lets go" on first run)
9. Plug in the Raspberry Pi Pico if you haven't already
10. Click in the bottom right, a menu should pop up. Select "MicroPython (Raspberry Pi Pico)"
11. Copy and paste the whole code snippet from [main.py](./main.py) into the Thonny IDE
12. Press the Green play button to upload the code to the Pi

10
main.py
View File

@ -9,10 +9,12 @@ cur_deg = 90
reset_button = Pin(14, Pin.IN)
calibrate_button = Pin(15, Pin.IN)
cal_threshold = 0
led = Pin('WL_GPIO0', Pin.OUT)
led = Pin(0, Pin.OUT)
led.high()
run_avg = [0, 0, 0, 0, 0]
sensitivity = 10
def servo(degrees):
if degrees > 180:
@ -36,10 +38,10 @@ def move_to_light():
is_facing_light = False
while not is_facing_light:
offset = light_sensor_1.read_u16() - light_sensor_2.read_u16()
if offset > 50 and cur_deg > 0:
if offset > (sensitivity - 5) and cur_deg > 0:
cur_deg = cur_deg - 1
servo(cur_deg - 5)
elif offset < -50 and cur_deg < 180:
elif offset < -(sensitivity - 5) and cur_deg < 180:
cur_deg = cur_deg + 1
servo(cur_deg)
else:
@ -72,7 +74,7 @@ while True:
sign = True
for i in run_avg:
cur_num = i
if abs(cur_num) > 75:
if abs(cur_num) > sensitivity:
if first_iter:
if cur_num >= 0:
sign = True

101
main.py.temp Normal file
View File

@ -0,0 +1,101 @@
import time
from machine import Pin, ADC, PWM
light_sensor_1 = ADC(26)
light_sensor_2 = ADC(27)
servo_pwm = PWM(Pin(28, Pin.OUT))
servo_pwm.freq(50)
cur_deg = 90
reset_button = Pin(14, Pin.IN)
calibrate_button = Pin(15, Pin.IN)
led = Pin(25, Pin.OUT)
led.high()
run_avg = [0, 0, 0, 0, 0]
light_1_offset = 0
light_2_offset = 0
def servo(degrees):
if degrees > 180:
degrees = 180
if degrees < 0:
degrees = 0
max_duty = 2500000
min_duty = 500000
new_duty = min_duty + (max_duty - min_duty) * (degrees / 180)
servo_pwm.duty_ns(int(new_duty))
def shift_array(arr, num_to_add):
arr.pop(0)
arr.append(num_to_add)
return arr
def move_to_light():
print("Moving to light...")
global cur_deg
is_facing_light = False
while not is_facing_light:
offset = light_sensor_1.read_u16() - light_sensor_2.read_u16()
if offset > 75 and cur_deg > 0:
cur_deg = cur_deg - 1
servo(cur_deg)
elif offset < -75 and cur_deg < 180:
cur_deg = cur_deg + 1
servo(cur_deg)
else:
is_facing_light = True
time.sleep(.01)
print("Moved to light!")
servo(90)
while True:
if reset_button.value() == 1:
led.low()
cur_deg = 90
servo(90)
cal_threshold = 0
run_avg = [0, 0, 0, 0, 0]
while reset_button.value() == 1:
time.sleep(.1)
led.high()
print("Reset")
continue
if calibrate_button.value() == 1:
sens_1 = light_sensor_1.read_u16()
sens_2 = light_sensor_2.read_u16()
if sens_1 > sens_2:
light_1_offset = sens_1 - sens_2
elif sens_1 < sens_2:
light_2_offset = sens_2 - sens_1
while calibrate_button.value() == 1:
time.sleep(.1)
diff = light_sensor_1.read_u16() - light_sensor_2.read_u16() - (light_1_offset - light_2_offset)
run_avg = shift_array(run_avg, diff)
num_over = 0
first_iter = True
sign = True
for cur_num in run_avg:
if abs(cur_num) > 90:
if first_iter:
print("First iter")
if cur_num >= 0:
sign = True
else:
sign = False
first_iter = False
if (cur_num >= 0 and not sign) or (cur_num < 0 and sign):
print("Reset")
num_over = 0
first_iter = True
break
elif (cur_num >= 0 and sign) or (cur_num < 0 and not sign):
print("+1")
num_over += 1
if num_over == 5:
print("Try move")
move_to_light()
run_avg = [0, 0, 0, 0, 0]