Compare commits
7 Commits
45404bb504
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d64ffda2d6 | ||
|
|
2d2befbbfd | ||
|
|
5188bf0f25 | ||
|
|
857af9857b | ||
|
|
aeeb78bf98 | ||
|
|
7bd18d09ac | ||
|
|
1058fd505f |
5
Makefile
Normal file
5
Makefile
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
sim:
|
||||||
|
python simulation.py
|
||||||
|
|
||||||
|
sync:
|
||||||
|
rsync -r --exclude=venv ~/solarmotor guest@hahn1.one:
|
||||||
32
README.md
32
README.md
@@ -13,3 +13,35 @@ from adafruit_servokit import ServoKit
|
|||||||
kit = ServoKit(channels=16)
|
kit = ServoKit(channels=16)
|
||||||
kit.servo[0].angle = 180
|
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
|
||||||
|
```
|
||||||
|
|||||||
61
clock.py
61
clock.py
@@ -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()
|
|
||||||
3
host.txt
3
host.txt
@@ -1,3 +0,0 @@
|
|||||||
Local addresses
|
|
||||||
inet 169.254.217.237/16
|
|
||||||
inet6 fe80::d89b:cecc:9c55:e1c3/64
|
|
||||||
@@ -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
0
objects/__init__.py
Normal file
15
objects/board.py
Normal file
15
objects/board.py
Normal 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
|
||||||
@@ -7,42 +7,40 @@ class Motor:
|
|||||||
MAX_PULSE = 2500
|
MAX_PULSE = 2500
|
||||||
MIN_PULSE = 500
|
MIN_PULSE = 500
|
||||||
COVERAGE = 180 # Total degree of freedom in degrees
|
COVERAGE = 180 # Total degree of freedom in degrees
|
||||||
AREA = MAX_PULSE - MIN_PULSE
|
OFFSET = 0 # In degrees a constant to be added
|
||||||
OFFSET = 2 # In degrees a constant to be added
|
|
||||||
SCALE = 1 # Scaling
|
SCALE = 1 # Scaling
|
||||||
|
|
||||||
# Used for ids
|
# Used for ids
|
||||||
count = 0
|
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.id = Motor.count; Motor.count += 1
|
||||||
self.pi = pi # Local pi instance
|
|
||||||
|
|
||||||
self.pin = pin
|
|
||||||
self.angle = angle
|
self.angle = angle
|
||||||
self.offset = Motor.OFFSET # Fine grained controls over every motor
|
self.offset = Motor.OFFSET # Fine grained controls over every motor
|
||||||
|
self.coverage = Motor.COVERAGE
|
||||||
self.scale = Motor.SCALE
|
self.scale = Motor.SCALE
|
||||||
|
|
||||||
# Initialization
|
# Initialization
|
||||||
self.set()
|
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
|
# Update the motor position on hardware
|
||||||
def set(self):
|
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):
|
def set_angle(self, angle):
|
||||||
self.angle = angle
|
self.angle = min(self.coverage, max(0, angle)) # Double check bad
|
||||||
self.set()
|
self.set()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"Motor {self.id} is set at {self.angle} degrees."
|
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):
|
def inc(self, inc):
|
||||||
self.angle += inc
|
self.angle += inc
|
||||||
self.angle = min(max(self.angle, 0), Motor.COVERAGE) # Clip
|
self.angle = min(max(self.angle, 0), Motor.COVERAGE) # Clip
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
import objects.motor as motor
|
||||||
|
|
||||||
class MovingEntity:
|
class MovingEntity:
|
||||||
"""Embedded entity in the world with a position."""
|
"""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 position rotated by world's tilt around y-axis."""
|
||||||
return self.world.rotate_point_y(self.pos)
|
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):
|
class Target(MovingEntity):
|
||||||
def __init__(self, world, pos=(0.0, 0.0, 0.0)):
|
def __init__(self, world, pos=(0.0, 0.0, 0.0)):
|
||||||
super().__init__(world)
|
super().__init__(world)
|
||||||
@@ -25,14 +29,14 @@ class Source(MovingEntity):
|
|||||||
self.pos = pos
|
self.pos = pos
|
||||||
|
|
||||||
class Mirror:
|
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.world = world
|
||||||
self.cluster_x = cluster_x
|
self.cluster_x = cluster_x
|
||||||
self.cluster_y = cluster_y
|
self.cluster_y = cluster_y
|
||||||
|
|
||||||
# Store the motors
|
# Store the motors
|
||||||
self.yaw = motor.Motor(self.world.pi, yaw_pin)
|
self.yaw = motor.Motor(self.world.board)
|
||||||
self.pitch = motor.Motor(self.world.pi, pitch_pin)
|
self.pitch = motor.Motor(self.world.board)
|
||||||
|
|
||||||
# Position in un-tilted coordinate system
|
# Position in un-tilted coordinate system
|
||||||
self.pos = (cluster_x * self.world.grid_size, cluster_y * self.world.grid_size, 0.0)
|
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
|
return self.yaw.angle, self.pitch.angle
|
||||||
|
|
||||||
class World:
|
class World:
|
||||||
def __init__(self, pi, tilt_deg=0.0):
|
def __init__(self, board, tilt_deg=0.0):
|
||||||
self.pi = pi
|
self.board = board
|
||||||
|
|
||||||
self.grid_size = 10 # In cm
|
self.grid_size = 10 # In cm
|
||||||
self.tilt_deg = tilt_deg # Tilt of the grid system around y-axis
|
self.tilt_deg = tilt_deg # Tilt of the grid system around y-axis
|
||||||
@@ -1,107 +1,56 @@
|
|||||||
import pigpio
|
|
||||||
import RPi.GPIO as GPIO
|
|
||||||
import time
|
import time
|
||||||
|
import math
|
||||||
|
|
||||||
# Solar module for simulation of world
|
# Solar module for simulation of world
|
||||||
import solar # Modeling of the world
|
import objects.solar as solar # Modeling of the world
|
||||||
from motor import Motor # Small helper functions and constants
|
|
||||||
|
|
||||||
# Constants
|
from objects.motor import Motor # Small helper functions and constants
|
||||||
SERVO1_PIN = 18
|
from objects.board import Board
|
||||||
SERVO2_PIN = 19
|
|
||||||
|
|
||||||
INIT_PULSE = 0
|
|
||||||
STEP = 10
|
STEP = 10
|
||||||
LOOP_DELAY = 0.01 # In seconds
|
LOOP_DELAY = 0.005 # 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)
|
|
||||||
|
|
||||||
# Testing embedding the mirrors in the world
|
# 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
|
HEIGHT = 30
|
||||||
|
|
||||||
source = solar.Source(world, pos=(0, 0, 30))
|
source = solar.Source(world, pos=(30, 50, 100))
|
||||||
target = solar.Target(world, pos=(20, 0, 30))
|
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 x in range(3):
|
||||||
for y in range(3):
|
for y in range(2):
|
||||||
mirror = solar.Mirror(world, cluster_x=x, cluster_y=y)
|
mirror = solar.Mirror(world, cluster_x=x, cluster_y=y)
|
||||||
world.add_mirror(mirror)
|
world.add_mirror(mirror)
|
||||||
|
|
||||||
world.update_mirrors_from_source_target(source, target)
|
world.update_mirrors_from_source_target(source, target)
|
||||||
|
|
||||||
for i, mirror in enumerate(world.mirrors):
|
def print_status():
|
||||||
|
for i, mirror in enumerate(world.mirrors):
|
||||||
pitch, yaw = mirror.get_angles()
|
pitch, yaw = mirror.get_angles()
|
||||||
print(f"Mirror {i} ({mirror.cluster_x}, {mirror.cluster_y}) angles -> pitch: {pitch:.2f}°, yaw: {yaw:.2f}°")
|
print(f"Mirror {i} ({mirror.cluster_x}, {mirror.cluster_y}) angles -> pitch: {pitch:.2f}°, yaw: {yaw:.2f}°")
|
||||||
|
|
||||||
def sm1(a): # Set motor 1
|
print_status()
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
|
a = 1
|
||||||
|
t = time.time()
|
||||||
|
|
||||||
# Main
|
# Main
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
# Shutdown
|
source.move(0, 0, 0.1)
|
||||||
if GPIO.input(SHUTDOWN_BTN) == GPIO.HIGH:
|
#source.move(10 * math.sin(a * t), 10 * math.cos(a * t))
|
||||||
os.system("sudo shutdown now")
|
print(source.pos)
|
||||||
|
print(target.pos)
|
||||||
|
|
||||||
pulse1 = angle_to_pulse(angle)
|
world.update_mirrors_from_source_target(source, target)
|
||||||
time.sleep(3)
|
print_status()
|
||||||
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)
|
|
||||||
|
|
||||||
time.sleep(LOOP_DELAY)
|
time.sleep(LOOP_DELAY)
|
||||||
|
|
||||||
|
t = time.time()
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
finally:
|
|
||||||
pi.set_servo_pulsewidth(SERVO1_PIN, 0)
|
|
||||||
pi.set_servo_pulsewidth(SERVO2_PIN, 0)
|
|
||||||
pi.stop()
|
|
||||||
GPIO.cleanup()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user