In the case of this particular class - let’s call it “head motion” - I want it to do these things:
-
When instantiated, head = head_motion(v_center, h_center)
where “v_center” and “h_center” are the established “calibration constants” that represent the desired “center” position in the horizontal and vertical directions.
-
Method “center()” will use those constants to move the head back to it’s “center” position.
-
Method “move(v_pos, h_pos)” will move the head to the absolute location defined by “v_pos” and “h_pos”.
-
Method “shake(offset_value)” will move the head back-and-forth in both planes, by adding and subtracting “offset” from “v_center” and “h_center”
Note that one of the implications of this is that “v_center” and “h_center” do NOT have to represent the absolute centered position of the servos, but can represent the desired center position. (i.e. If the robot chassis is angled slightly, the “v_position” value may change slightly so that the camera is truly vertical and the scene directly in front of the 'bot is properly centered in the vertical plane even if that means the vertical servo isn’t set to perfectly mechanically centered.)
Secondary Goal: All methods, except for “move()” would work by passing the appropriate values to “move()” internally.
Stretch Goal:
The “zero offset” values passed at instantiation become calibration offset values such that move(90, 90)
becomes move(v_center, h_center)
by calculating the offset required to make the absolute x/y values, (referenced to 90/90) be internally referenced relative to v_center/h_center.
I think I will have to define the class something like this:
class head_movement(self, v, h):
def __init__(self, v, h):
self.v_center = int(v) #capture the calibration constants
self.h_center = int(h)
self.v_pos = self.v_center # set the initial values to centered values
self.h_pos = self.h_center
self.offset = int(0) # cast "zero" as an integer value.
[somehow or other, incorporate the servo methods, such that
self.servoN becomes the same as "servoN" in EasyGoPiGo or EasySensors]
def move(self, v = v_center, h = h_center); # pass default values if not set
servo1.rotate_servo(hpos)
servo2.rotate_servo(vpos)
sleep(0.25)
servo1.disable_servo()
servo2.disable_servo()
return(0) # is this necessary?
def center(self):
self.move()
def shake(self, offset = 0)
servo1.rotate_servo(hcenter + offset)
sleep(0.25)
servo1.rotate_servo(hcenter - offset)
sleep(0.25)
servo1.rotate_servo(h_center)
sleep(0.25)
servo2.rotate_servo(v_center + offset)
sleep(0.25)
servo2.rotate_servo(v_center - offset)
sleep(0.25)
servo2.rotate_servo(vcenter)
servo1.disable_servo()
servo2.disable_servo()
return(0)
# end class definition
I am sure I am leaving out 90% of what I need.
Can you give me hints on what I have to do?
I am sure that, somewhere before I define the class, I have to
-
from easysensors import servo
(or)
-
from easygopigo3 import EasyGoPiGo3
(and)
from time import sleep
My question is, (within the context of defining a class), how do I “instantiate”, (include), the content of the included class(es) so that their methods become part of the defined class without super-classing them? Do I do something like:
from easygopigo3 import EasyGoPiGo3
foo = EasyGoPiGo3()
servo1 = foo.init_servo('SERVO1')
servo2 = foo.init_servo('SERVO2')
class head_movement(self, v, h):
def __init__(self, v, h):
self.v_center = int(v) #capture the calibration constants
self.h_center = int(h)
self.v_pos = self.v_center # set the initial values to centered values
self.h_pos = self.h_center
self.offset = int(0) # cast "zero" as an integer value.
[somehow or other, incorporate the servo methods, such that
self.servoN becomes the same as "servoN" in EasyGoPiGo or EasySensors]
def move(self, v = v_center, h = h_center); # pass default values if not set
servo1.rotate_servo(hpos)
servo2.rotate_servo(vpos)
sleep(0.25)
servo1.disable_servo()
servo2.disable_servo()
return(0) # is this necessary?
[the rest of the class goes here]
I am also assuming that, were I to include the class inside a program in toto, it “inherits”, (can use), things that were already imported and/or defined prior to the class definition.