umstellen auf pigpio

This commit is contained in:
Bernd Reuther 2021-03-05 15:09:21 +01:00
parent e1b7352d91
commit 56dfc2251e
5 changed files with 141 additions and 88 deletions

View File

@ -1,24 +1,28 @@
import RPi.GPIO as GPIO
from flask import Flask, render_template, request
import time
import pigpio
pi = pigpio.pi()
from src.einstellungen import *
from src.schrittmotor import motor
import time
kamera_server = Flask(__name__)
# Set each pin as an output and make it low:
for pin in pins_out:
GPIO.setup(pins_out[pin]['nr'], GPIO.OUT)
GPIO.output(pins_out[pin]['nr'], GPIO.HIGH)
pi.set_mode(pins_out[pin]['nr'], pigpio.OUTPUT)
pi.write(pins_out[pin]['nr'], 1)
#GPIO.setup(pins_out[pin]['nr'], GPIO.OUT)
#GPIO.output(pins_out[pin]['nr'], GPIO.HIGH)
for pin in pins_in:
GPIO.setup(pins_in[pin]['nr'], GPIO.IN)
pi.set_mode(pins_out[pin]['nr'], pigpio.INPUT)
#GPIO.setup(pins_in[pin]['nr'], GPIO.IN)
GPIO.output(pins_out['kamera']['nr'], GPIO.LOW)
GPIO.setup(20, GPIO.OUT)
m3_pwm4 = GPIO.PWM(20, freq)
m3_pwm4.start(0)
#GPIO.output(pins_out['kamera']['nr'], GPIO.LOW)
pi.write(pins_out['kamera']['nr'], 0)
m1 = motor('drehen', m1_a1, m1_b1, m1_a2, m1_b2, m1_t1, m1_t2, m1_te, m1_es, 1, 180)
m2 = motor('kippen', m2_a1, m2_b1, m2_a2, m2_b2, m2_t1, m2_t2, m2_te, m2_es, 1, 90)
@ -37,9 +41,11 @@ m1.vorwaerts(100)
def main():
# For each pin, read the pin state and store it in the pins dictionary:
for pin in pins_out:
pins_out[pin]['state'] = GPIO.input(pins_out[pin]['nr'])
#pins_out[pin]['state'] = GPIO.input(pins_out[pin]['nr'])
pins_out[pin]['state'] = pi.read(pins_out[pin]['nr'])
for pin in pins_in:
pins_in[pin]['state'] = GPIO.input(pins_in[pin]['nr'])
#pins_in[pin]['state'] = GPIO.input(pins_in[pin]['nr'])
pins_in[pin]['state'] = pi.read(pins_in[pin]['nr'])
# Put the pin dictionary into the template data dictionary:
templateData = {
@ -60,18 +66,22 @@ def action(changePin, action):
# If the action part of the URL is "on," execute the code indented below:
if action == "on":
# Set the pin high:
GPIO.output(changePin, GPIO.HIGH)
#GPIO.output(changePin, GPIO.HIGH)
pi.write(changePin, 1)
# Save the status message to be passed into the template:
message = "Turned " + deviceName + " on."
if action == "off":
GPIO.output(changePin, GPIO.LOW)
#GPIO.output(changePin, GPIO.LOW)
pi.write(changePin, GPIO.LOW)
message = "Turned " + deviceName + " off."
# For each pin, read the pin state and store it in the pins dictionary:
for pin in pins_out:
pins_out[pin]['state'] = GPIO.input(pins_out[pin]['nr'])
#pins_out[pin]['state'] = GPIO.input(pins_out[pin]['nr'])
pins_out[pin]['state'] = pi.read(pins_out[pin]['nr'])
for pin in pins_in:
pins_in[pin]['state'] = GPIO.input(pins_in[pin]['nr'])
#pins_in[pin]['state'] = GPIO.input(pins_in[pin]['nr'])
pins_in[pin]['state'] = pi.read(pins_in[pin]['nr'])
# Along with the pin dictionary, put the message into the template data dictionary:
templateData = {
@ -84,8 +94,8 @@ def action(changePin, action):
@kamera_server.route("/position/<motor>/<position>")
def motor_test(motor, position):
if motor == "drehen":
m1.stop()
m1.start()
#m1.stop()
#m1.start()
m1.gehe_zu(int(position))
return 'Drehen: ' + str(m1.pos)

View File

@ -11,7 +11,31 @@ sudo apt update
sudo apt upgrade
```
### Konfiguration anpassen:
## Zusatzprogramme installieren
```
sudo apt install mc git zbar-tools build-essential python-dev nginx pigpio python3-pip
```
## gpio-server aktivieren
Service Datei anpassen `/lib/systemd/system/pigpiod.service.d/public.conf`:
```
[Service]
ExecStart=
ExecStart=/usr/bin/pigpiod -s1
```
Dienst aktivieren/starten
```
sudo systemctl daemon-reload
sudo systemctl enable pigpiod
sudo systemctl start pigpiod
```
## Konfiguration anpassen:
```
sudo raspi-config
@ -20,12 +44,7 @@ sudo raspi-config
- Zeitzone anpassen
- Passwort ändern
- Spracheinstellungen ändern
## Zusatzprogramme installieren
```
sudo apt install mc git zbar-tools build-essential python-dev nginx
```
- remote gpio erlauben
## Benutzer anlegen

View File

@ -1,21 +1,16 @@
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
freq = 800
freq_halten = 30000
cyc_halten = 12
cyc_halten = 20
pins_out = {
'kamera' : {'nr' : 2, 'name' : 'Relais, Kamera', 'state' : GPIO.HIGH},
'encoder' : {'nr' : 3, 'name' : 'Relais, Encoder', 'state' : GPIO.HIGH}
'kamera' : {'nr' : 2, 'name' : 'Relais, Kamera', 'state' : True},
'encoder' : {'nr' : 3, 'name' : 'Relais, Encoder', 'state' : True}
}
pins_in = {
'endschalter1' : {'nr' : 23, 'name' : 'Endschalter, Motor 1', 'state' : GPIO.HIGH},
'endschalter2' : {'nr' : 26, 'name' : 'Endschalter, Motor 2', 'state' : GPIO.HIGH},
'endschalter3' : {'nr' : 25, 'name' : 'Endschalter, Motor 3', 'state' : GPIO.HIGH}
'endschalter1' : {'nr' : 23, 'name' : 'Endschalter, Motor 1', 'state' : True},
'endschalter2' : {'nr' : 26, 'name' : 'Endschalter, Motor 2', 'state' : True},
'endschalter3' : {'nr' : 25, 'name' : 'Endschalter, Motor 3', 'state' : True}
}
m1_a1 = 17

View File

@ -1,11 +1,12 @@
from src.einstellungen import *
import RPi.GPIO as GPIO
#import RPi.GPIO as GPIO
import time
import pigpio
import threading
import math
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#GPIO.setmode(GPIO.BCM)
#GPIO.setwarnings(False)
class motor():
@ -31,32 +32,48 @@ class motor():
self.pwm4 = 0
def start(self):
GPIO.setup(self.sp1, GPIO.OUT)
GPIO.setup(self.sp2, GPIO.OUT)
GPIO.setup(self.sp3, GPIO.OUT)
GPIO.setup(self.sp4, GPIO.OUT)
pi.set_mode(self.sp1, pigpio.OUTPUT)
pi.set_mode(self.sp2, pigpio.OUTPUT)
pi.set_mode(self.sp3, pigpio.OUTPUT)
pi.set_mode(self.sp4, pigpio.OUTPUT)
#GPIO.setup(self.sp1, GPIO.OUT)
#GPIO.setup(self.sp2, GPIO.OUT)
#GPIO.setup(self.sp3, GPIO.OUT)
#GPIO.setup(self.sp4, GPIO.OUT)
self.pwm1 = GPIO.PWM(self.sp1, self.freq)
self.pwm1.start(0)
pi.set_PWM_range(self.sp1, 100)
pi.set_PWM_range(self.sp2, 100)
pi.set_PWM_range(self.sp3, 100)
pi.set_PWM_range(self.sp4, 100)
self.pwm2 = GPIO.PWM(self.sp2, self.freq)
self.pwm2.start(0)
pi.set_PWM_frequency(self.sp1, self.freq)
pi.set_PWM_dutycycle(self.sp1, 0)
self.pwm3 = GPIO.PWM(self.sp3, self.freq)
self.pwm3.start(0)
#self.pwm1 = GPIO.PWM(self.sp1, self.freq)
#self.pwm1.start(0)
pi.set_PWM_frequency(self.sp2, self.freq)
pi.set_PWM_dutycycle(self.sp2, 0)
#self.pwm2 = GPIO.PWM(self.sp2, self.freq)
#self.pwm2.start(0)
pi.set_PWM_frequency(self.sp3, self.freq)
pi.set_PWM_dutycycle(self.sp3, 0)
#self.pwm3 = GPIO.PWM(self.sp3, self.freq)
#self.pwm3.start(0)
if self.halten == 1:
self.pwm4 = GPIO.PWM(self.sp4, self.freq_halten)
self.pwm4.start(cyc_halten)
pi.set_PWM_frequency(self.sp4, self.freq_halten)
pi.set_PWM_dutycycle(self.sp4, cyc_halten)
#self.pwm4 = GPIO.PWM(self.sp4, self.freq_halten)
#self.pwm4.start(cyc_halten)
else:
self.pwm4 = GPIO.PWM(self.sp4, self.freq)
self.pwm4.start(0)
def stop(self):
self.pwm1.stop()
self.pwm2.stop()
self.pwm3.stop()
self.pwm4.stop()
pi.set_PWM_frequency(self.sp4, self.freq)
pi.set_PWM_dutycycle(self.sp4, 0)
#self.pwm4 = GPIO.PWM(self.sp4, self.freq)
#self.pwm4.start(0)
def vorwaerts(self, schritte, eichen=0):
ges_schritte = schritte * 8
@ -69,7 +86,8 @@ class motor():
temp_t2 = self.t2
if self.halten == 1:
self.pwm4.ChangeFrequency(self.freq)
pi.set_PWM_frequency(self.sp4, self.freq)
#self.pwm4.ChangeFrequency(self.freq)
for i in range(int(schritte)):
if self.pos >= self.schritte_max:
@ -78,22 +96,25 @@ class motor():
self.pos += 1
temp_pos = i * 8
einzelschritt(self.pwm1, 1, ges_schritte, temp_pos, temp_t1, temp_t2)
einzelschritt(self.pwm4, 0, ges_schritte, temp_pos + 1, temp_t1, temp_t2)
einzelschritt(self.pwm2, 1, ges_schritte, temp_pos + 2, temp_t1, temp_t2)
einzelschritt(self.pwm1, 0, ges_schritte, temp_pos + 3, temp_t1, temp_t2)
einzelschritt(self.pwm3, 1, ges_schritte, temp_pos + 4, temp_t1, temp_t2)
einzelschritt(self.pwm2, 0, ges_schritte, temp_pos + 5, temp_t1, temp_t2)
einzelschritt(self.pwm4, 1, ges_schritte, temp_pos + 6, temp_t1, temp_t2)
einzelschritt(self.pwm3, 0, ges_schritte, temp_pos + 7, temp_t1, temp_t2)
einzelschritt(self.sp1, 1, ges_schritte, temp_pos, temp_t1, temp_t2)
einzelschritt(self.sp4, 0, ges_schritte, temp_pos + 1, temp_t1, temp_t2)
einzelschritt(self.sp2, 1, ges_schritte, temp_pos + 2, temp_t1, temp_t2)
einzelschritt(self.sp1, 0, ges_schritte, temp_pos + 3, temp_t1, temp_t2)
einzelschritt(self.sp3, 1, ges_schritte, temp_pos + 4, temp_t1, temp_t2)
einzelschritt(self.sp2, 0, ges_schritte, temp_pos + 5, temp_t1, temp_t2)
einzelschritt(self.sp4, 1, ges_schritte, temp_pos + 6, temp_t1, temp_t2)
einzelschritt(self.sp3, 0, ges_schritte, temp_pos + 7, temp_t1, temp_t2)
#print(str(self.pos))
if self.halten == 1:
self.pwm4.ChangeFrequency(self.freq_halten)
self.pwm4.ChangeDutyCycle(self.cyc_halten)
pi.set_PWM_frequency(self.sp4, self.freq_halten)
pi.set_PWM_dutycycle(self.sp4, cyc_halten)
#self.pwm4.ChangeFrequency(self.freq_halten)
#self.pwm4.ChangeDutyCycle(self.cyc_halten)
else:
self.pwm4.ChangeDutyCycle(0)
pi.set_PWM_dutycycle(self.sp4, 0)
#self.pwm4.ChangeDutyCycle(0)
def rueckwaerts(self, schritte, eichen=0):
ges_schritte = schritte * 8
@ -106,10 +127,12 @@ class motor():
temp_t2 = self.t2
if self.halten == 1:
self.pwm4.ChangeFrequency(self.freq)
pi.set_PWM_frequency(self.sp4, self.freq)
#self.pwm4.ChangeFrequency(self.freq)
for i in range(int(schritte)):
if GPIO.input(self.endschalter) == GPIO.LOW:
#if GPIO.input(self.endschalter) == GPIO.LOW:
if pi.read(self.endschalter) == 0:
print('Endschalter ' + self.name)
self.pos = 0
break
@ -117,25 +140,29 @@ class motor():
self.pos -= 1
temp_pos = i * 8
einzelschritt(self.pwm3, 1, ges_schritte, temp_pos, temp_t1, temp_t2)
einzelschritt(self.pwm4, 0, ges_schritte, temp_pos + 1, temp_t1, temp_t2)
einzelschritt(self.pwm2, 1, ges_schritte, temp_pos + 2, temp_t1, temp_t2)
einzelschritt(self.pwm3, 0, ges_schritte, temp_pos + 3, temp_t1, temp_t2)
einzelschritt(self.pwm1, 1, ges_schritte, temp_pos + 4, temp_t1, temp_t2)
einzelschritt(self.pwm2, 0, ges_schritte, temp_pos + 5, temp_t1, temp_t2)
einzelschritt(self.pwm4, 1, ges_schritte, temp_pos + 6, temp_t1, temp_t2)
einzelschritt(self.pwm1, 0, ges_schritte, temp_pos + 7, temp_t1, temp_t2)
einzelschritt(self.sp3, 1, ges_schritte, temp_pos, temp_t1, temp_t2)
einzelschritt(self.sp4, 0, ges_schritte, temp_pos + 1, temp_t1, temp_t2)
einzelschritt(self.sp2, 1, ges_schritte, temp_pos + 2, temp_t1, temp_t2)
einzelschritt(self.sp3, 0, ges_schritte, temp_pos + 3, temp_t1, temp_t2)
einzelschritt(self.sp1, 1, ges_schritte, temp_pos + 4, temp_t1, temp_t2)
einzelschritt(self.sp2, 0, ges_schritte, temp_pos + 5, temp_t1, temp_t2)
einzelschritt(self.sp4, 1, ges_schritte, temp_pos + 6, temp_t1, temp_t2)
einzelschritt(self.sp1, 0, ges_schritte, temp_pos + 7, temp_t1, temp_t2)
#print(str(self.pos))
if self.halten == 1:
self.pwm4.ChangeFrequency(self.freq_halten)
self.pwm4.ChangeDutyCycle(self.cyc_halten)
pi.set_PWM_frequency(self.sp4, self.freq_halten)
pi.set_PWM_dutycycle(self.sp4, cyc_halten)
#self.pwm4.ChangeFrequency(self.freq_halten)
#self.pwm4.ChangeDutyCycle(self.cyc_halten)
else:
self.pwm4.ChangeDutyCycle(0)
pi.set_PWM_dutycycle(self.sp4, 0)
#self.pwm4.ChangeDutyCycle(0)
def eichen(self):
if GPIO.input(self.endschalter) == GPIO.LOW:
#if GPIO.input(self.endschalter) == GPIO.LOW:
if pi.read(self.endschalter) == 0:
self.vorwaerts(10, 1)
self.rueckwaerts(self.schritte_max, 1)
@ -158,10 +185,12 @@ def einzelschritt(spule, status, ges_schritte, schritt, t1, t2):
if status == 1:
for i in range(0, 91, 5):
dc = round(math.sin(math.radians(i)) * 100, 0)
spule.ChangeDutyCycle(dc)
pi.set_PWM_dutycycle(spule, dc)
#spule.ChangeDutyCycle(dc)
time.sleep(t)
else:
for i in range(0, 91, 5):
dc = round(math.cos(math.radians(i)) * 100, 0)
spule.ChangeDutyCycle(dc)
pi.set_PWM_dutycycle(spule, dc)
#spule.ChangeDutyCycle(dc)
time.sleep(t)

View File

@ -12,7 +12,7 @@
<h1>RPi Web Server</h1>
{% for pin in pins %}
<h2>{{ pins[pin].name }}
{% if pins[pin].state == true %}
{% if pins[pin].state == 1 %}
ist gerade <strong>an</strong></h2><div class="row"><div class="col-md-2">
<a href="/{{pin}}/off" class="btn btn-block btn-lg btn-default" role="button">Turn off</a></div></div>
{% else %}
@ -22,7 +22,7 @@
{% endfor %}
{% for pin in pins_in %}
<h3>{{ pins_in[pin].name }}
{% if pins_in[pin].state == true %}
{% if pins_in[pin].state == 1 %}
ist gerade <strong>an</strong></h3>
{% else %}
ist gerade <strong>aus</strong></h3>