Greetings!
Forgive the brevity of this posting, the software involved is rather long and I’d rather not make this posting five miles long by including it. So, I will try to summarize the best I can from memory.
Given:
A series of Python programs that contain a lot of the exact same code.
I cut the code, (some of which simply defines what should be global constants, other parts contain routines that are used throughout the code.)
In the main part of the code I have a whole bunch of include statements.
from EasyGoPiGo3 include EasyGoPiGo3
from global_constants include *
from head_motion include *
global_constants contains:
##############################
### Basic Global Constants ###
##############################
MAX_FORCE = 5.0
MIN_SPEED = 0.0 # forces a minimum speed if force > 0
MAX_SPEED = 500.0
force_multiplier = 1 # allows a slower, smoother startup if > 1
drive_constant = (MAX_SPEED - MIN_SPEED) / (force_multiplier * MAX_FORCE)
# calibration constants for the servo center position which are
# determined experimentally by visual inspection of the servos
# TODO: Create a servo calibration routine that is a part of the
# control panel and saves these settings in the gpg3_config.json file.
# This will allow these calibraton constants to be globally
# available to any process that wants to use them.
#
# Initially the vposition and hposition of the two servos
# is set to vcenter and hcenter respectively, then hposition and vposition
# are incremented/decremented to move the servos as commanded
vcenter = vposition = 93 # tilt charlie's head up slightly
hcenter = hposition = 93
# Set the movement step size
servo_step_size = 5
# Directory Path can change depending on where you install this file. Non-standard installations
# may require you to change this directory.
directory_path = '/home/pi/Project_Files/Projects/RemoteCameraRobot/static'
##################################
### End Basic Global Constants ###
##################################
head_motion contains:
# File Head_Motion
#
# This file provides the global routines for creating
# instances of the servo class and the functions needed
# to move the robots head in whatever direction is needed
#
# Add instantiate "servo" object
servo1 = gopigo3_robot.init_servo('SERVO1')
servo2 = gopigo3_robot.init_servo('SERVO2')
# Generic "head movement" routine
def move_head(hpos, vpos):
servo1.rotate_servo(hpos)
servo2.rotate_servo(vpos)
sleep(0.25)
servo1.disable_servo()
servo2.disable_servo()
return(0)
# Center Charlie's head
def center_head():
global vposition
global vcenter
global hposition
global hcenter
vposition = vcenter
hposition = hcenter
move_head(hposition, vposition)
return(0)
# Shake Charlie's head - just to prove he's alive! ;)
def shake_head():
vpos = 88
hpos = 97
logging.info("Shaking Charlie's Head From Side To Side\n")
hpos = 110
move_head(hpos, vpos)
hpos = 84
move_head(hpos, vpos)
logging.info("Centering Charlie's head horizontally\n")
center_head()
logging.info("Moving Charlie's Head Up And Down\n")
vpos = 110
move_head(hpos, vpos)
vpos = 66
move_head(hpos, vpos)
logging.info("Re-centering Charlie's head vertically\n")
center_head()
return(0)
All of the global includes:
import signal
import sys
import logging
from time import sleep
from easygopigo3 import EasyGoPiGo3
exist in the main program before I import “global_includes” or “head_motion”
My understanding of importing things in Python, particularly “from ‘X’ import *”, was that the entire thing was imported into the current namespace, almost as if it were a macro, even though you don’t see it in the code itself.
When I try to do something:
# Add instantiate "servo" object
servo1 = gopigo3_robot.init_servo('SERVO1')
servo2 = gopigo3_robot.init_servo('SERVO2')
I get errors like “gopigo_robot” doesn’t exist in ‘servo1 = gopigo3_robot.init_servo(‘SERVO1’)’
If I don’t break anything out into modules and include everything in-line, in the same file, everything works wonderfully.
The conclusion I am coming to is that maybe these things aren’t in the same namespace? Do I have to include all the supporting libraries and instantiate everything over and over and over again, every time I want to use something in a different module?
If I have global variables, (global foo, global barr, global bazz), do I have to declare them as global in every module that I import? That’s what I want to do in “global_constants”, declare everything that’s global and give them sane initial values, once and once only in one module that I can modify to change it globally throughout the code.
This is what I am trying to avoid; having tons of repeated code in every copy of everything I’m doing.