Compare commits

...

5 Commits

Author SHA1 Message Date
Dlr Rpi
d64ffda2d6 makefile 2025-11-13 12:33:51 +01:00
Dlr Rpi
2d2befbbfd Debug 2025-11-06 17:11:33 +01:00
Dlr Rpi
5188bf0f25 Update the cables 2025-11-06 17:01:28 +01:00
Dlr Rpi
857af9857b first test 2025-11-06 16:56:20 +01:00
Dlr Rpi
aeeb78bf98 Changed structure 2025-11-06 16:20:44 +01:00
13 changed files with 98 additions and 359 deletions

5
Makefile Normal file
View File

@@ -0,0 +1,5 @@
sim:
python simulation.py
sync:
rsync -r --exclude=venv ~/solarmotor guest@hahn1.one:

View File

@@ -13,3 +13,35 @@ from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)
kit.servo[0].angle = 180
```
```text
https://pinout.xyz/ Full pinout for the rpi3
https://components101.com/sites/default/files/component_datasheet/SG90%20Servo%20Motor%20Datasheet.pdf Datasheet for the servomotor used
https://ben.akrin.com/raspberry-pi-servo-jitter/ Blog post how to fix jittering
```
Local address
inet6 fe80::7e2c:ada5:5de7:9a2c/64
## Cables
From right to left
```
..............................
. ---------- 1x .
. 2x .
. 3x .
. xx .
. ooooo 4x .
. ooooo xx .
. ooooo xx .
. xx .
. xx .
. .
1: white
2: purlple
3: yellow
4: black
```

View File

@@ -1,61 +0,0 @@
import pigpio
import RPi.GPIO as GPIO
import time
import os
from motor import Motor # Models the motor
# Constants
SERVO1_PIN = 18
SERVO2_PIN = 19
BUTTON1_FWD = 5
BUTTON1_BWD = 6
BUTTON2_FWD = 17
BUTTON2_BWD = 27
SHUTDOWN_BTN = 26
STEP = 2
LOOP_DELAY = 0.3 # In seconds
# Local pi
pi = pigpio.pi()
if not pi:
os.exit()
# Setup the controls
GPIO.setmode(GPIO.BCM)
for btn in [BUTTON1_FWD, BUTTON1_BWD, BUTTON2_FWD, BUTTON2_BWD, SHUTDOWN_BTN]:
GPIO.setup(btn, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Setup motors
m1 = Motor(pi, SERVO1_PIN)
m2 = Motor(pi, SERVO2_PIN)
# Main
try:
while True:
# Inputs shutdown
if GPIO.input(SHUTDOWN_BTN) == GPIO.HIGH:
os.system("sudo shutdown now")
# Motors
if GPIO.input(BUTTON1_FWD):
m1.inc(STEP)
elif GPIO.input(BUTTON1_BWD):
m1.inc(-STEP)
if GPIO.input(BUTTON2_FWD):
m2.inc(STEP)
elif GPIO.input(BUTTON2_BWD):
m2.inc(-STEP)
time.sleep(LOOP_DELAY)
except KeyboardInterrupt:
pass
finally:
del m1
del m2
pi.stop()
GPIO.cleanup()

View File

@@ -1,41 +0,0 @@
# https://learn.adafruit.com/16-channel-pwm-servo-driver/python-circuitpython
import time
from adafruit_servokit import ServoKit
MIN = 550
MAX = 2450
CHANNELS = 16
kit = ServoKit(channels=CHANNELS, frequency=50)
for i in range(CHANNELS):
kit.servo[i].angle = 0
time.sleep(2)
# Testing the accuracy of the lib
num = 14
kit.servo[num].angle = 0
time.sleep(2)
kit.servo[num].angle = 90
time.sleep(2)
kit.servo[num].angle = 30
time.sleep(2)
kit.servo[num].angle = 120
time.sleep(2)
kit.servo[num].angle = 60
time.sleep(2)
kit.servo[num].angle = 150
time.sleep(1.5)
kit.servo[num].angle = 90
time.sleep(1.5)
kit.servo[num].angle = 180

View File

@@ -1,96 +0,0 @@
import time
from adafruit_servokit import ServoKit
class MultiSmoothServoController:
def __init__(self, channels=16, min_pulse=500, max_pulse=2500):
self.kit = ServoKit(channels=channels)
self.channels = channels
self.min_pulse = min_pulse
self.max_pulse = max_pulse
# Track last angle and pulse per servo
self._last_angles = [None] * channels
self._last_pulses = [None] * channels
# Set pulse width range for all channels upfront
for ch in range(channels):
self.kit.servo[ch].set_pulse_width_range(min_pulse, max_pulse)
def angle_to_pulse(self, angle):
"""Convert angle (0-180) to pulse width in microseconds"""
return int(self.min_pulse + (self.max_pulse - self.min_pulse) * angle / 180)
def set_angle(self, channel, angle):
"""Set angle for a single servo channel, avoid jitter"""
if channel < 0 or channel >= self.channels:
raise ValueError(f"Channel must be between 0 and {self.channels - 1}")
if angle < 0 or angle > 180:
raise ValueError("Angle must be between 0 and 180")
pulse = self.angle_to_pulse(angle)
if self._last_angles[channel] != angle or self._last_pulses[channel] != pulse:
self.kit.servo[channel].pulse_width = pulse
self._last_angles[channel] = angle
self._last_pulses[channel] = pulse
def smooth_move(self, channel, start_angle, end_angle, step=1, delay=0.02):
"""Smoothly move one servo from start_angle to end_angle"""
if start_angle < end_angle:
angles = range(start_angle, end_angle + 1, step)
else:
angles = range(start_angle, end_angle - 1, -step)
for angle in angles:
self.set_angle(channel, angle)
time.sleep(delay)
def smooth_move_all(self, start_angles, end_angles, step=1, delay=0.02):
"""
Smoothly move all servos from their respective start_angles to end_angles.
Both start_angles and end_angles should be lists/tuples of length = number of channels.
"""
if len(start_angles) != self.channels or len(end_angles) != self.channels:
raise ValueError("start_angles and end_angles must have length equal to number of channels")
max_steps = 0
# Calculate how many steps each servo needs and track max
steps_per_servo = []
for start, end in zip(start_angles, end_angles):
steps = abs(end - start) // step
steps_per_servo.append(steps)
if steps > max_steps:
max_steps = steps
for i in range(max_steps + 1):
for ch in range(self.channels):
start = start_angles[ch]
end = end_angles[ch]
if start < end:
angle = min(start + i * step, end)
else:
angle = max(start - i * step, end)
self.set_angle(ch, angle)
time.sleep(delay)
def stop_all(self):
"""Stop pulses to all servos (optional)"""
for ch in range(self.channels):
self.kit.servo[ch].pulse_width = 0
self._last_angles[ch] = None
self._last_pulses[ch] = None
controller = MultiSmoothServoController()
# Move servo channel 2 smoothly from 0 to 180 degrees
controller.smooth_move(channel=2, start_angle=0, end_angle=180)
# Smoothly move multiple servos simultaneously:
start_positions = [0] * 16 # all servos at 0°
end_positions = [90, 45, 180, 0, 30, 60, 90, 120, 150, 180, 0, 90, 45, 135, 180, 0]
controller.smooth_move_all(start_positions, end_positions)
# Stop all servos
controller.stop_all()

View File

@@ -1,3 +0,0 @@
Local addresses
inet 169.254.217.237/16
inet6 fe80::d89b:cecc:9c55:e1c3/64

View File

@@ -1,3 +0,0 @@
https://pinout.xyz/ Full pinout for the rpi3
https://components101.com/sites/default/files/component_datasheet/SG90%20Servo%20Motor%20Datasheet.pdf Datasheet for the servomotor used
https://ben.akrin.com/raspberry-pi-servo-jitter/ Blog post how to fix jittering

0
objects/__init__.py Normal file
View File

15
objects/board.py Normal file
View File

@@ -0,0 +1,15 @@
from adafruit_servokit import ServoKit
class Board:
MIN = 500
MAX = 2500
def __init__(self, channels=16, frequency=50):
self.channels = channels
self.frequency = frequency
self.kit = ServoKit(channels=channels, frequency=frequency)
for i in range(channels):
self.kit.servo[i].set_pulse_width_range(Board.MIN, Board.MAX)
self.kit.servo[i].actuation_range = 180
self.kit.servo[i].angle = 0

View File

@@ -1,11 +1,5 @@
"""Helpers for building moving mirrors."""
from adafruit_servokit import ServoKit
class Board:
def __init__(self, channels=16, frequency=50):
self.kit = ServoKit(channels=channels, frequency=frequency)
class Motor:
"""Model a type of servo motor."""
@@ -13,42 +7,40 @@ class Motor:
MAX_PULSE = 2500
MIN_PULSE = 500
COVERAGE = 180 # Total degree of freedom in degrees
AREA = MAX_PULSE - MIN_PULSE
OFFSET = 2 # In degrees a constant to be added
OFFSET = 0 # In degrees a constant to be added
SCALE = 1 # Scaling
# Used for ids
count = 0
def __init__(self, pi, pin, angle=0):
def __init__(self, board, angle=0):
self.board = board
self.id = Motor.count; Motor.count += 1
self.pi = pi # Local pi instance
self.pin = pin
self.angle = angle
self.offset = Motor.OFFSET # Fine grained controls over every motor
self.coverage = Motor.COVERAGE
self.scale = Motor.SCALE
# Initialization
self.set()
def angle_to_pulse(self, angle):
return min(max(Motor.MIN_PULSE, (Motor.MIN_PULSE + Motor.AREA * angle/Motor.COVERAGE + self.offset) * self.scale), Motor.MAX_PULSE)
# Update the motor position on hardware
def set(self):
self.pi.set_servo_pulsewidth(self.pin, self.angle_to_pulse(self.angle))
self.board.kit.servo[self.id].angle = self.angle * self.scale + self.offset
def safe_set_angle(angle=0, sleep=0.01, offset=1):
kit.servo[NUM].angle = angle + offset
time.sleep(sleep)
kit.servo[NUM].angle = angle
def set_angle(self, angle):
self.angle = angle
self.angle = min(self.coverage, max(0, angle)) # Double check bad
self.set()
def __str__(self):
return f"Motor {self.id} is set at {self.angle} degrees."
def __del__(self):
self.pi.set_servo_pulsewidth(self.pin, 0)
def inc(self, inc):
self.angle += inc
self.angle = min(max(self.angle, 0), Motor.COVERAGE) # Clip

View File

@@ -2,6 +2,7 @@
"""
import math
import objects.motor as motor
class MovingEntity:
"""Embedded entity in the world with a position."""
@@ -14,6 +15,9 @@ class MovingEntity:
"""Return position rotated by world's tilt around y-axis."""
return self.world.rotate_point_y(self.pos)
def move(self, dx=0, dy=0, dz=0):
self.pos = (self.pos[0] + dx, self.pos[1] + dy, self.pos[2] + dz)
class Target(MovingEntity):
def __init__(self, world, pos=(0.0, 0.0, 0.0)):
super().__init__(world)
@@ -25,14 +29,14 @@ class Source(MovingEntity):
self.pos = pos
class Mirror:
def __init__(self, world, pitch_pin, yaw_pin, cluster_x=0, cluster_y=0):
def __init__(self, world, cluster_x=0, cluster_y=0):
self.world = world
self.cluster_x = cluster_x
self.cluster_y = cluster_y
# Store the motors
self.yaw = motor.Motor(self.world.pi, yaw_pin)
self.pitch = motor.Motor(self.world.pi, pitch_pin)
self.yaw = motor.Motor(self.world.board)
self.pitch = motor.Motor(self.world.board)
# Position in un-tilted coordinate system
self.pos = (cluster_x * self.world.grid_size, cluster_y * self.world.grid_size, 0.0)
@@ -81,8 +85,8 @@ class Mirror:
return self.yaw.angle, self.pitch.angle
class World:
def __init__(self, pi, tilt_deg=0.0):
self.pi = pi
def __init__(self, board, tilt_deg=0.0):
self.board = board
self.grid_size = 10 # In cm
self.tilt_deg = tilt_deg # Tilt of the grid system around y-axis

View File

@@ -1,107 +1,56 @@
import pigpio
import RPi.GPIO as GPIO
import time
import math
# Solar module for simulation of world
import solar # Modeling of the world
from motor import Motor # Small helper functions and constants
import objects.solar as solar # Modeling of the world
# Constants
SERVO1_PIN = 18
SERVO2_PIN = 19
from objects.motor import Motor # Small helper functions and constants
from objects.board import Board
INIT_PULSE = 0
STEP = 10
LOOP_DELAY = 0.01 # In seconds
pi = pigpio.pi()
if not pi.connected:
print("Cannot connect to pigpio daemon!")
exit()
angle1 = init_motor(SERVO1_PIN)
angle2 = init_motor(SERVO2_PIN)
LOOP_DELAY = 0.005 # In seconds
# Testing embedding the mirrors in the world
world = solar.World(tilt_deg=15) # The world is tilted 15 degrees around y-axis
board = Board()
world = solar.World(board, tilt_deg=15) # The world is tilted 15 degrees around y-axis
HEIGHT = 30
source = solar.Source(world, pos=(0, 0, 30))
target = solar.Target(world, pos=(20, 0, 30))
source = solar.Source(world, pos=(30, 50, 100))
target = solar.Target(world, pos=(0, 0, 40))
# Create mirrors in a 9x9 grid
# Create mirrors in a 3x2 grid
for x in range(3):
for y in range(3):
for y in range(2):
mirror = solar.Mirror(world, cluster_x=x, cluster_y=y)
world.add_mirror(mirror)
world.update_mirrors_from_source_target(source, target)
def print_status():
for i, mirror in enumerate(world.mirrors):
pitch, yaw = mirror.get_angles()
print(f"Mirror {i} ({mirror.cluster_x}, {mirror.cluster_y}) angles -> pitch: {pitch:.2f}°, yaw: {yaw:.2f}°")
def sm1(a): # Set motor 1
pi.set_servo_pulsewidth(SERVO1_PIN, angle_to_pulse(a))
time.sleep(2)
angle = 150
time.sleep(10)
sm1(150)
sm1(90)
sm1(0)
sm1(180)
sm1(30)
sm1(120)
sm1(60)
sm1(30)
sm1(180)
sm1(120)
sm1(30)
sm1(150)
sm1(3)
print_status()
a = 1
t = time.time()
# Main
try:
while True:
# Shutdown
if GPIO.input(SHUTDOWN_BTN) == GPIO.HIGH:
os.system("sudo shutdown now")
source.move(0, 0, 0.1)
#source.move(10 * math.sin(a * t), 10 * math.cos(a * t))
print(source.pos)
print(target.pos)
pulse1 = angle_to_pulse(angle)
time.sleep(3)
angle += 10
# Motor 1
target1 = pulse1
if GPIO.input(BUTTON1_FWD):
target1 = min(MAX_PULSE, pulse1 + STEP)
elif GPIO.input(BUTTON1_BWD):
target1 = max(MIN_PULSE, pulse1 - STEP)
pulse1 = move_servo(pulse1, target1)
pi.set_servo_pulsewidth(SERVO1_PIN, pulse1)
# Motor 2
target2 = pulse2
if GPIO.input(BUTTON2_FWD):
target2 = min(MAX_PULSE, pulse2 + STEP)
elif GPIO.input(BUTTON2_BWD):
target2 = max(MIN_PULSE, pulse2 - STEP)
pulse2 = move_servo(pulse2, target2)
pi.set_servo_pulsewidth(SERVO2_PIN, pulse2)
world.update_mirrors_from_source_target(source, target)
print_status()
time.sleep(LOOP_DELAY)
t = time.time()
except KeyboardInterrupt:
pass
finally:
pi.set_servo_pulsewidth(SERVO1_PIN, 0)
pi.set_servo_pulsewidth(SERVO2_PIN, 0)
pi.stop()
GPIO.cleanup()

54
test.py
View File

@@ -1,54 +0,0 @@
import time
from adafruit_servokit import ServoKit
# This is set to zero for the max range
OFFSET = -10
MIN = 500 + OFFSET
MAX = 2500 - OFFSET
CHANNELS = 16
RANGE = 180
INIT = 0
FREQUENCY = 50
# Setup the kit
kit = ServoKit(channels=CHANNELS, frequency=FREQUENCY)
for i in range(CHANNELS):
kit.servo[i].set_pulse_width_range(MIN, MAX)
kit.servo[i].actuation_range = RANGE
kit.servo[i].angle = INIT
time.sleep(2)
# Testing the accuracy of the lib
NUM = 8
OFFSET = 5
# Set the angle
def sa(angle=0, sleep=1.5):
print(f"Set angle of pin {NUM+1} to {angle}.")
kit.servo[NUM].angle = angle + OFFSET
time.sleep(0.01)
kit.servo[NUM].angle = angle
kit.servo[NUM].angle = angle - OFFSET
time.sleep(0.01)
kit.servo[NUM].angle = angle
time.sleep(sleep)
sa(80)
exit()
#for i in range(180):
#sa(i, 0)
#time.sleep(0.1)
# Run the actual testing
sa(10)
sa(90)
sa(30)
sa(170)
sa(60)
sa(150)
sa(90, 0)