-
Notifications
You must be signed in to change notification settings - Fork 1
/
electronics_testing_10.29.24.py
132 lines (104 loc) · 3.81 KB
/
electronics_testing_10.29.24.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# IMPORTS
import pigpio
import time
import RPi.GPIO as GPIO #imports servo library
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
import csv
# SERVO SETUP -----------------------
# Initialize pigpio
pi = pigpio.pi('localhost', 8888)
# Define GPIO pins connected to the servos
servo_gpio_pin_1 = 19
servo_gpio_pin_2 = 18
# Set servo pulse width limits (in microseconds)
servo_min_pulse = 900
servo_max_pulse = 2100
# Sleep time
sleep_time = 1
# Function to move servo to a specific angle
def move_servo(servo_pin, angle):
pulse_width = servo_min_pulse + (servo_max_pulse - servo_min_pulse) * (angle / 180)
pi.set_servo_pulsewidth(servo_pin, pulse_width)
# LIGHTS SETUP ----------------------------
# code to program lights
# figure out difference between duty cycle and
GPIO.setmode(GPIO.BCM)
GPIO_PIN = 24
# Set the GPIO pin as an output
GPIO.setup(GPIO_PIN, GPIO.OUT)
# PWM?? - mimmum pwm signal
pwm = GPIO.PWM(GPIO_PIN, 100)
# Sleep time setting
sleep_time = 1
#pwm.start()
#GPIO.output(GPIO_pin, GPIO.HIGH)
#pwm.start(11)
#13-17 is when the light is brightest, further testing for brightest signal
pwm.start(11) # starts the signal at the minmum value (11-19)
time.sleep(4)
# PRESSURE SETUP ---------------------------------
# Initialize the I2C interface
i2c = busio.I2C(board.SCL, board.SDA)
# Create an ADS 1115 object
ads = ADS.ADS1115(i2c)
# Define the analog input channel
channel1 = AnalogIn(ads, ADS.P0)
channel2 = AnalogIn(ads, ADS.P1)
# var for formula
pressure_range = 450
voltage_upper = 5
voltage_lower = 0
# RUNNING LOOP -------------------------------------
try:
while True:
# Example: Sweep from 0 to 180 degrees for servo 1
for angle in range(0, 180, 180):
move_servo(servo_gpio_pin_2, angle)
move_servo(servo_gpio_pin_1, angle)
time.sleep(sleep_time)
# Sweep back from 180 to 0 degrees for servo 1
for angle in range(180, 0, -180):
move_servo(servo_gpio_pin_2, angle)
move_servo(servo_gpio_pin_1, angle)
time.sleep(sleep_time)
# lights cycling code
pwm.ChangeDutyCycle(0)
time.sleep(sleep_time)
pwm.ChangeDutyCycle(19)
time.sleep(sleep_time)
pwm.ChangeDutyCycle(0)
# pressure logging code
voltage_reading1 = channel1.voltage
old_pressure1 = channel1.value / 1023 * 5
pressure1 = (pressure_range * (voltage_reading1 - voltage_lower) ) / (voltage_upper - voltage_lower)
voltage_reading2 = channel2.voltage
old_pressure2 = channel2.value / 1023 * 5
pressure2 = (pressure_range * (voltage_reading2 - voltage_lower) ) / (voltage_upper - voltage_lower)
data = [
{"Old Pressure1": old_pressure1,
"Channel Value1": channel1.value,
"Voltage1": voltage_reading1,
"Pressure1": pressure1,
"Old Pressure2": old_pressure2,
"Channel Value2": channel2.value,
"Voltage2": voltage_reading2,
"Pressure2": pressure2}
]
with open('test.csv', 'a', newline='') as csvfile:
fieldnames = ['Old Pressure1', 'Channel Value1', 'Voltage1', 'Pressure1',
'Old Pressure2', 'Channel Value2', 'Voltage2', 'Pressure2']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data)
time.sleep(0.5)
except KeyboardInterrupt:
# Ctrl+C pressed, cleanup GPIO
pi.set_servo_pulsewidth(servo_gpio_pin_1, 0) # Stop servo 1
pi.set_servo_pulsewidth(servo_gpio_pin_2, 0) # Stop servo 2
pi.stop() # Close pigpio connection
# Clean up GPIO
pwm.stop()
GPIO.cleanup()