[Dorset] Next Meeting - One Week tonight

2019-06-25 Thread Terry Coles
Hi,

The next meeting is one week tonight on Tuesday, 2019-07-02 20:00 at the 
Bournemouth Electric Club.  See:

http://dorset.lug.org.uk/wiki/doku.php?id=meetings:pub#bournemouth_electric

Paul, Hamish,

Are you going? 

-- 



Terry Coles



-- 
  Next meeting: BEC, Bournemouth, Tuesday, 2019-07-02 20:00
  Check to whom you are replying
  Meetings, mailing list, IRC, ...  http://dorset.lug.org.uk/
  New thread, don't hijack:  mailto:dorset@mailman.lug.org.uk


Re: [Dorset] Programming problem: Have I done something wrong?

2019-06-25 Thread PeterMerchant via dorset



And the GPIO clean-up is after that definition, just before you decide
to hand control over to Flask by calling app.run().


GPIO.cleanup()

if __name__ == '__main__':
      app.run(debug=True, host='0.0.0.0')

You're cleaning up the GPIO before Flask gets to run and that probably
undoes all your GPIO and PWM configuration.  Assuming run() can return,
I forget, move the clean up:

 if __name__ == '__main__':
 app.run(debug=True, host='0.0.0.0')
 GPIO.cleanup()


Thanks Ralph, Things happen now.

Peter


--
 Next meeting: BEC, Bournemouth, Tuesday, 2019-07-02 20:00
 Check to whom you are replying
 Meetings, mailing list, IRC, ...  http://dorset.lug.org.uk/
 New thread, don't hijack:  mailto:dorset@mailman.lug.org.uk


Re: [Dorset] Programming problem: Have I done something wrong?

2019-06-25 Thread Ralph Corderoy
Hi Peter,

> This quite long because the guy that wrote it documented every step.

And because this chunk in the middle is repeated.  :-)

> frequency_hertz = 50
> pwm = GPIO.PWM(pin_number, frequency_hertz)
>
>
> # How to position a servo?  All servos are pretty much the same.
> # Send repeated purses of an absolute duration (not a relative duty cycle)
> # between 0.40 ms and 2.5 ms in duration.  A single pulse will only move it
> # a short distance in the desired direction.  Repeated pulses will continue
> # its movement and then once it arrives at the specified position it will
> # insruct the motor to forcefully hold its position.
> left_position = 0.40
> right_position = 2.5
> middle_position = (right_position - left_position) / 2 + left_position
>
> # I'll store a sequence of positions for use in a loop later on.
> positionList = [left_position, middle_position, right_position, 
> middle_position]
>
> # total number of milliseconds in a a cycle.  Given this, we will then
> # know both how long we want to pulse in this cycle and how long tghe
> # cycle itself is.  That is all we need to calculate a duty cycle as
> # a percentage.
> ms_per_cycle = 1000 / frequency_hertz
>

Note his ordering of using `pwm' in the loop, stopping afterwards, and
then `cleaning up' GPIO.

> for i in range(3):
>     for position in positionList:
>     duty_cycle_percentage = position * 100 / ms_per_cycle
>     pwm.start(duty_cycle_percentage)
>     time.sleep(.5)
>
> pwm.stop()
> GPIO.cleanup()

And compare it with yours where the `pwm' is in a callback function to
handle the URL,

> @app.route('/motor-forwards/')
> def motorforwards():
>      duty_cycle_percentage = left_position * 100 / ms_per_cycle
>      pwm.start(duty_cycle_percentage)
>      time.sleep(.5)
>      pwm.stop()
>      return render_template('index.html')

And the GPIO clean-up is after that definition, just before you decide
to hand control over to Flask by calling app.run().

> GPIO.cleanup()
>
> if __name__ == '__main__':
>      app.run(debug=True, host='0.0.0.0')

You're cleaning up the GPIO before Flask gets to run and that probably
undoes all your GPIO and PWM configuration.  Assuming run() can return,
I forget, move the clean up:

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
GPIO.cleanup()

-- 
Cheers, Ralph.

-- 
  Next meeting: BEC, Bournemouth, Tuesday, 2019-07-02 20:00
  Check to whom you are replying
  Meetings, mailing list, IRC, ...  http://dorset.lug.org.uk/
  New thread, don't hijack:  mailto:dorset@mailman.lug.org.uk


[Dorset] Programming problem: Have I done something wrong?

2019-06-25 Thread PeterMerchant via dorset

This is to do with driving an R/C Servo from a Raspberry Pi.  I have this 
working with motors driving my car, but this takes two pins at opposing 
polarities for change of direction. An R/C servo works differently: Instead of 
two wires of opposite polarity for the motor direction there is only one 
control wire using pulse width modulation between 1 and 2 milliseconds for full 
control.

An HTML program calls the python program to drive the car as here:

- -

#  2018-04-05 Modified from Les Pounder program to handle two motors
# one for steering and one for motion
# steering is Left = 10, right = 11
# changing it trying to add Turn for motor control
# 2018-10-31 changing toggle LED to stop motors

from flask import Flask, render_template
from gpiozero import LED, Motor
from time import sleep

# led = LED(25)
motor = Motor(forward=18, backward=23)
turn  = Motor(forward=10, backward=11)


app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/motor-stop/')
def on():
    motor.stop()
    return render_template('index.html')

@app.route('/motor-forwards/')
# 31/1/19 modified to test double push on button to stop. see notes.
def motorforwards():
    if motor.value == 0:
    motor.forward()
    elif motor.value > 0:
    motor.stop()
    return render_template('index.html')

@app.route('/motor-backwards/')
def motorbackwards():
    motor.backward()
    return render_template('index.html')

@app.route('/motor-left/')
def motorleft():
    turn.forward()
    sleep(1)
    turn.stop()
    return render_template('index.html')

@app.route('/motor-right/')
def motorright():
    turn.backward()
    sleep(1)
    turn.stop()
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')


--- -- 

A python test program that drives the Servos, and works. This quite long 
because the guy that wrote it documented every step.

-- -- -


# This gives us control of the Raspberry Pi's pins.
import RPi.GPIO as GPIO

# This is only used for time delays... standard Python stuff.
import time

# Tell i which pin number we'll  be using to refer to the GPIO pains.
# We will use the physical pin ordering.
GPIO.setmode(GPIO.BOARD)

# We will tell the Broadcom CPU which pins do what.
# There are many pins and most have up to 5 different functions,
# each with a default.  Check the pinout to find non-specialized
# "GPIO" pins.  We'll use P!-Pin_11 (using BOARD reference),
# which is the same as GPIO17 (Broadcom / BCM reference).
# We need our pin to use the GPIO digital output function, so we just
# tell it to designate this pin for OUTPUT.
pin_number = 18
GPIO.setup(pin_number, GPIO.OUT)

# Now we can use PWM on pin 18.  It's software PWM, so don't expect perfect
# results.  Linux is a multitasking OS so other processes could interrupt
# the process which generate the PWM signal at any time.
# Raspberry Pi has a hardware PWm channel, but this Pythong library
# does not yet support it.
frequency_hertz = 50
pwm = GPIO.PWM(pin_number, frequency_hertz)


# How to position a servo?  All servos are pretty much the same.
# Send repeated purses of an absolute duration (not a relative duty cycle)
# between 0.40 ms and 2.5 ms in duration.  A single pulse will only move it
# a short distance in the desired direction.  Repeated pulses will continue
# its movement and then once it arrives at the specified position it will
# insruct the motor to forcefully hold its position.
left_position = 0.40
right_position = 2.5
middle_position = (right_position - left_position) / 2 + left_position

# I'll store a sequence of positions for use in a loop later on.
positionList = [left_position, middle_position, right_position, middle_position]

# total number of milliseconds in a a cycle.  Given this, we will then
# know both how long we want to pulse in this cycle and how long tghe
# cycle itself is.  That is all we need to calculate a duty cycle as
# a percentage.
ms_per_cycle = 1000 / frequency_hertz

frequency_hertz = 50
pwm = GPIO.PWM(pin_number, frequency_hertz)


# How to position a servo?  All servos are pretty much the same.
# Send repeated purses of an absolute duration (not a relative duty cycle)
# between 0.40 ms and 2.5 ms in duration.  A single pulse will only move it
# a short distance in the desired direction.  Repeated pulses will continue
# its movement and then once it arrives at the specified position it will
# insruct the motor to forcefully hold its position.
left_position = 0.40
right_position = 2.5
middle_position = (right_position - left_position) / 2 + left_position

# I'll store a sequence of positions for use in a loop later on.
positionList = [left_position, middle_position, right_position, middle_position]

# total number of milliseconds in a a cycle.  Given this, we will then
# know both how long we want to pulse in this cycle and how long tghe
# cycle itself