Python Forum

Full Version: project model railroad python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would realy like some help with a project
I have asked for help in a couple of places
I think it might be better to outline the whole project

What it does
Computer control of points/turnouts using servos on a model railway/railroad
To do it cheaply, with control through a browser
(so control via laptop, smart phone or other wifi divice)

I have hardware ready to test it

What I can not do is figure out how to write the python program I need to run it

Now that I have mentioned model railways and programing you are the only person left reading this

There are 3 parts
Part 1
The browser
Basically this would just be a pic on the screen with some icons (radio buttons)
when you click them they plus or minus a number from a variable
since there will be no more than 16 icons in a group the variable can be a hex pair or a decimal 0-255
There can be more than one variable
when ever this value changes it should be sent to the main program
I think this is a job for json (I do not know how to do this) HELP HERE please

Part 2
User settings
A simple editable file (probably a .py)
this file contains 33 values
How far a servo travels clockwise x16
How far a servo travels anticlockwise x16
and aone to set the speed the servos travel

something like this:-
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  servo_limits.py
#  
# Changes in this file will not take effect until you restart the interpreter
 
# Set the speed at which the servos move
# Bigger number is slower
# servo_speed = 10   # For later use


# Configure min and max servo pulse lengths
servo_0_min = 150  # Min pulse length out of 4096
servo_0_max = 600  # Max pulse length out of 4096
servo_1_min = 150  # Min pulse length out of 4096
servo_1_max = 600  # Max pulse length out of 4096
servo_2_min = 150  # Min pulse length out of 4096
servo_2_max = 600  # Max pulse length out of 4096
servo_3_min = 150  # Min pulse length out of 4096
servo_3_max = 600  # Max pulse length out of 4096
servo_4_min = 150  # Min pulse length out of 4096
servo_4_max = 600  # Max pulse length out of 4096
servo_5_min = 150  # Min pulse length out of 4096
servo_5_max = 600  # Max pulse length out of 4096
servo_6_min = 150  # Min pulse length out of 4096
servo_6_max = 600  # Max pulse length out of 4096
servo_7_min = 150  # Min pulse length out of 4096
servo_7_max = 600  # Max pulse length out of 4096
servo_8_min = 150  # Min pulse length out of 4096
servo_8_max = 600  # Max pulse length out of 4096
servo_9_min = 150  # Min pulse length out of 4096
servo_9_max = 600  # Max pulse length out of 4096
servo_10_min = 150  # Min pulse length out of 4096
servo_10_max = 600  # Max pulse length out of 4096
servo_11_min = 150  # Min pulse length out of 4096
servo_11_max = 600  # Max pulse length out of 4096
servo_12_min = 150  # Min pulse length out of 4096
servo_12_max = 600  # Max pulse length out of 4096
servo_13_min = 150  # Min pulse length out of 4096
servo_13_max = 600  # Max pulse length out of 4096
servo_14_min = 150  # Min pulse length out of 4096
servo_14_max = 600  # Max pulse length out of 4096
servo_15_min = 150  # Min pulse length out of 4096
servo_15_max = 600  # Max pulse length out of 4096


# DON'T FORGET TO RESTART THE INTERPRETER

  Part 3
           The main program
A: setting up

It' something like this:-
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  first.py
#  
#  Copyright 2018 Colin <colin@whitesnake>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  


#def main(args):
#    return 0

#if __name__ == '__main__':
#    import sys
#    sys.exit(main(sys.argv))


# Simple demo of of the PCA9685 PWM servo/LED controller library.
# This will move channel 0 from min to max position repeatedly.
# Author: Tony DiCola
# License: Public Domain
from __future__ import division
import time

# Import the PCA9685 module.
import Adafruit_PCA9685

# Import servo_limits
# This sets the end points of the servo sweep
import servo_limits

# Uncomment to enable debug output.
#import logging
#logging.basicConfig(level=logging.DEBUG)

# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685()

# Alternatively specify a different address and/or bus:
#pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2)

(  # Configure min and max servo pulse lengths
(  servo_min = 150  # Min pulse length out of 4096               THIS BIT IS WRONG
(  servo_max = 600  # Max pulse length out of 4096   IT NEEDS TO GET ITS SETTINGS FROM THE USER FILE

# Helper function to make setting a servo pulse width simpler.
def set_servo_pulse(channel, pulse):
    pulse_length = 1000000    # 1,000,000 us per second
    pulse_length //= 60       # 60 Hz
    print('{0}us per period'.format(pulse_length))
    pulse_length //= 4096     # 12 bits of resolution
    print('{0}us per bit'.format(pulse_length))
    pulse *= 1000
    pulse //= pulse_length
    pwm.set_pwm(channel, 0, pulse)

# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
B: The loop
This is the bit that does the work
first
print('Moving servo on channel 0, press Ctrl-C to quit...') some kind of stop function
While (start loop)
next get input from json (if it is there)
this is the bit where my logic gets confused (oh to be vulcan)
first time around this is chanel 0
if position=>min pos (from servo_limits) servo pos = min
if position=>min pos (from servo_limits) and bit1 (from json) =1 then servo positoin+1

ok this is where I get confused
if the coresponding digit from the json is 1 add 1 until max (from servo_limits is reached)
if the servo is at max and json is 0 then keep reducing position by -1 until min(from servo_limits is reached)
at the end of the loop
add 1 to the channel number
If chanel number < 15 channel number =0
loop back
HELP HOW DO I DO THIS?