本文整理汇总了Python中PyMata.pymata.PyMata.analog_write方法的典型用法代码示例。如果您正苦于以下问题:Python PyMata.analog_write方法的具体用法?Python PyMata.analog_write怎么用?Python PyMata.analog_write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyMata.pymata.PyMata
的用法示例。
在下文中一共展示了PyMata.analog_write方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PyMata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import analog_write [as 别名]
This example illustrates using callbacks for digital and analog input.
Monitor the current analog input and digital input of two pins.
"""
import signal
import sys
import time
from PyMata.pymata import PyMata
# Instantiate PyMata
board = PyMata("/dev/ttyACM0", verbose=True)
board.encoder_config(7,2)
# Set up pin modes for both pins with callbacks for each
board.set_pin_mode(5, board.PWM, board.DIGITAL)
board.set_pin_mode(6, board.PWM, board.DIGITAL)
board.analog_write(5, 255)
board.analog_write(6, 0)
time.sleep(2)
count = board.digital_read(2)
print count
time.sleep(2)
board.analog_write(5, 0)
示例2: ArduinoFirmata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import analog_write [as 别名]
class ArduinoFirmata(Board, AnalogInputCapable, PwmOutputCapable):
def __init__(self, port=None):
try:
from PyMata.pymata import PyMata as PyMata # noqa
except ImportError:
msg = 'pingo.arduino.Arduino requires PyMata installed'
raise ImportError(msg)
super(ArduinoFirmata, self).__init__()
self.port = port
self.firmata_client = PyMata(self.port, verbose=VERBOSE)
self.firmata_client.capability_query()
time.sleep(10) # TODO: Find a small and safe value
capability_query_results = self.firmata_client.get_capability_query_results()
capability_dict = pin_list_to_board_dict(capability_query_results)
self._add_pins(
[DigitalPin(self, location)
for location in capability_dict['digital']] +
[PwmPin(self, location)
for location in capability_dict['pwm']] +
[AnalogPin(self, 'A%s' % location, resolution=10)
for location in capability_dict['analog']]
)
def cleanup(self):
# self.firmata_client.close() has sys.exit(0)
if hasattr(self, 'PyMata'):
try:
self.firmata_client.transport.close()
except AttributeError:
pass
def __repr__(self):
cls_name = self.__class__.__name__
return '<{cls_name} {self.port!r}>'.format(**locals())
def _set_digital_mode(self, pin, mode):
self.firmata_client.set_pin_mode(
pin.location,
PIN_MODES[mode],
self.firmata_client.DIGITAL
)
def _set_analog_mode(self, pin, mode):
pin_id = int(pin.location[1:])
self.firmata_client.set_pin_mode(
pin_id,
self.firmata_client.INPUT,
self.firmata_client.ANALOG
)
def _set_pwm_mode(self, pin, mode):
pin_id = int(pin.location)
self.firmata_client.set_pin_mode(
pin_id,
self.firmata_client.PWM,
self.firmata_client.DIGITAL
)
def _get_pin_state(self, pin):
_state = self.firmata_client.digital_read(pin.location)
if _state == self.firmata_client.HIGH:
return pingo.HIGH
return pingo.LOW
def _set_pin_state(self, pin, state):
self.firmata_client.digital_write(
pin.location,
PIN_STATES[state]
)
def _get_pin_value(self, pin):
pin_id = int(pin.location[1:])
return self.firmata_client.analog_read(pin_id)
def _set_pwm_duty_cycle(self, pin, value):
pin_id = int(pin.location)
firmata_value = int(value * 255)
return self.firmata_client.analog_write(pin_id, firmata_value)
def _set_pwm_frequency(self, pin, value):
raise NotImplementedError
示例3: PyMata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import analog_write [as 别名]
# create a PyMata instance
board = PyMata("/dev/ttyACM0")
def signal_handler(sig, frame):
print('You pressed Ctrl+C!!!!')
if board is not None:
board.reset()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
# control the servo - note that you don't need to set pin mode
# configure the servo
board.servo_config(SERVO_MOTOR)
while (1):
# move the servo to 20 degrees
board.analog_write(SERVO_MOTOR, 20)
time.sleep(1)
# move the servo to 100 degrees
board.analog_write(SERVO_MOTOR, 100)
time.sleep(1)
# move the servo to 20 degrees
board.analog_write(SERVO_MOTOR, 20)
# close the interface down cleanly
board.close()
示例4: PyMata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import analog_write [as 别名]
FADESPEED = 5
# Create an instance of PyMata.
SERIAL_PORT = "/dev/ttyS0"
firmata = PyMata( SERIAL_PORT, max_wait_time=5 )
# initialize the digital pin as an output.
firmata.set_pin_mode( REDPIN, firmata.PWM, firmata.DIGITAL)
firmata.set_pin_mode( GREENPIN, firmata.PWM, firmata.DIGITAL)
firmata.set_pin_mode( BLUEPIN, firmata.PWM, firmata.DIGITAL)
try:
# run in a loop over and over again forever:
while True:
firmata.analog_write( BLUEPIN, 0 )
for bright in range (0,255,8):
firmata.analog_write( GREENPIN, bright )
firmata.analog_write( REDPIN, bright )
# firmata.analog_write( GREENPIN, 255 )
# firmata.analog_write( BLUEPIN, 255 )
time.sleep(0.1) # wait for a second
except KeyboardInterrupt:
# Catch exception raised by using Ctrl+C to quit
pass
# close the interface down cleanly
firmata.close()
示例5: print
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import analog_write [as 别名]
signal.signal(signal.SIGINT, signal_handler)
# set pin modes
board.set_pin_mode(GREEN_LED, board.OUTPUT, board.DIGITAL)
board.set_pin_mode(RED_LED, board.PWM, board.DIGITAL)
board.set_pin_mode(PUSH_BUTTON, board.INPUT, board.DIGITAL)
board.set_pin_mode(POTENTIOMETER, board.INPUT, board.ANALOG)
board.set_analog_latch(POTENTIOMETER, board.ANALOG_LATCH_GTE, 1000)
# do nothing loop - program exits when latch data event occurs for potentiometer
while 1:
count += 1
if count == 300:
print('bye bye')
board.close()
analog = board.analog_read(POTENTIOMETER)
board.analog_write(RED_LED, analog // 4)
digital = board.digital_read(PUSH_BUTTON)
board.digital_write(GREEN_LED, digital)
latch = board.get_analog_latch_data(POTENTIOMETER)
if latch[1] == board.LATCH_LATCHED:
board.analog_write(RED_LED, 0)
board.digital_write(GREEN_LED, 0)
print('Latching Event Occurred at: ' + time.asctime(time.gmtime(latch[3])))
board.close()
sys.exit(0)
示例6: speed
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import analog_write [as 别名]
gforce = conn.add_stream(getattr, flight, "g_force")
speed = conn.add_stream(getattr, flight, "speed")
control = vessel.control
sas = conn.add_stream(getattr, control, "sas")
gear = conn.add_stream(getattr, control, "gear")
lights = conn.add_stream(getattr, control, "lights")
brakes = conn.add_stream(getattr, control, "brakes")
# Your Application Continues Below This Point
print "We are go!"
while 1 :
s = speed()
if s > 400:
s = 400
board.analog_write(SPEEDOMETER, int(s * 255 / 400))
f = 0
mf = maxFuel()
if mf > 0 :
f = fuel() / mf
board.analog_write(FUELGAUGE, int(f * 255))
g = math.fabs(gforce()) / 40
if g > 1:
g = 1
board.analog_write(GFORCEMETER, int(g * 255))
board.digital_write(SASLAMP, sas())
board.digital_write(GEARLAMP, gear())
board.digital_write(LAMPLAMP, lights())
示例7: FSMHand
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import analog_write [as 别名]
class FSMHand():
def __init__(self):
# Initialize fingers and wrist pos
self.t_pos = 0
self.i_pos = 0
self.m_pos = 0
self.r_pos = 0
self.l_pos = 0
self.w_pos = 90
# Initialize sensor values
self.t_sen = 0
self.i_sen = 0
self.m_sen = 0
self.r_sen = 0
self.l_sen = 0
# Initialize the arduino
self.arduino = PyMata("/dev/ttyACM0", verbose=True)
# Initialize the servos and sensors on arduino
for pin in SERVO_PINS:
self.arduino.servo_config(pin)
sensor_pin = 0
self.arduino.enable_analog_reporting(sensor_pin)
sensor_pin += 1
# Initialize the hand states
self.curstate = 'open'
self.states = {}
self.transitionTable = {}
#states are a dictionary of name/function pairints stored in a dictionary
#i.e. {'open':self.Open}
def AddFSMState(self,name,function):
self.states[name] = function
#each state gets its own transition table
#a transition table is a list of states to switch to
#given a "event"
def AddFSMTransition(self,name,transitionDict):
#yes we are making a dictionary the value bound to a dictionary key
self.transitionTable[name] = transitionDict
def move_callback(self, data):
servo_pose = data.finger_pose
if self.curstate == 'open':
self.arduino.analog_write(MIDDLE_SERVO, servo_pose)
self.m_pos = servo_pose
rospy.loginfo(rospy.get_caller_id() + " I heard %d", servo_pose)
def RunFSM(self):
pub = rospy.Publisher('finger_status', Pressure, queue_size=10)
rate = rospy.Rate(50)
while not rospy.is_shutdown():
self.m_sen = self.arduino.analog_read(THUMB_SENSOR)
outdata = Pressure()
outdata.sensor1 = self.m_sen
pub.publish(outdata)
if self.m_sen > 500 or self.m_pos == 181:
self.curstate = 'close'
else:
self.curstate = 'open'
print "Current State: ", self.curstate
rate.sleep()
示例8: Motors
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import analog_write [as 别名]
class Motors(Thread):
MOTOR_1_PWM = 2
MOTOR_1_A = 3
MOTOR_1_B = 4
MOTOR_2_PWM = 5
MOTOR_2_A = 6
MOTOR_2_B = 7
MOTOR_3_PWM = 8
MOTOR_3_A = 9
MOTOR_3_B = 10
def __init__(self):
Thread.__init__(self)
self.daemon = True
self.board = PyMata()
def signal_handler(sig, frame):
self.stop_motors()
self.board.reset()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
self.board.set_pin_mode(self.MOTOR_1_PWM, self.board.PWM, self.board.DIGITAL)
self.board.set_pin_mode(self.MOTOR_1_A, self.board.OUTPUT, self.board.DIGITAL)
self.board.set_pin_mode(self.MOTOR_1_B, self.board.OUTPUT, self.board.DIGITAL)
self.board.set_pin_mode(self.MOTOR_2_PWM, self.board.PWM, self.board.DIGITAL)
self.board.set_pin_mode(self.MOTOR_2_A, self.board.OUTPUT, self.board.DIGITAL)
self.board.set_pin_mode(self.MOTOR_2_B, self.board.OUTPUT, self.board.DIGITAL)
self.board.set_pin_mode(self.MOTOR_3_PWM, self.board.PWM, self.board.DIGITAL)
self.board.set_pin_mode(self.MOTOR_3_A, self.board.OUTPUT, self.board.DIGITAL)
self.board.set_pin_mode(self.MOTOR_3_B, self.board.OUTPUT, self.board.DIGITAL)
self.dx, self.dy = 0, 0
def stop_motors(self):
self.board.digital_write(self.MOTOR_1_B, 0)
self.board.digital_write(self.MOTOR_1_A, 0)
self.board.digital_write(self.MOTOR_2_B, 0)
self.board.digital_write(self.MOTOR_2_A, 0)
self.board.digital_write(self.MOTOR_3_B, 0)
self.board.digital_write(self.MOTOR_3_A, 0)
def run(self):
while True:
# Reset all direction pins to avoid damaging H-bridges
# TODO: USE dx,dy now in (-1,1)+(None,None) range
self.stop_motors()
dist = abs(self.dx)
if dist > 0.2: #was 2
if self.dx > 0:
print("Turning left")
self.board.digital_write(self.MOTOR_1_B, 1)
self.board.digital_write(self.MOTOR_2_B, 1)
self.board.digital_write(self.MOTOR_3_B, 1)
else:
print("Turning right")
self.board.digital_write(self.MOTOR_1_A, 1)
self.board.digital_write(self.MOTOR_2_A, 1)
self.board.digital_write(self.MOTOR_3_A, 1)
self.board.analog_write(self.MOTOR_1_PWM, int(dist ** 0.7 + 25))
self.board.analog_write(self.MOTOR_2_PWM, int(dist ** 0.7 + 25))
self.board.analog_write(self.MOTOR_3_PWM, int(dist ** 0.7 + 25))
# elif self.dy > 30:
else:
print("Going forward")
self.board.digital_write(self.MOTOR_1_B, 1)
self.board.digital_write(self.MOTOR_3_A, 1)
self.board.analog_write(self.MOTOR_1_PWM, int(self.dy ** 0.5 )+30)
self.board.analog_write(self.MOTOR_2_PWM, 0)
self.board.analog_write(self.MOTOR_3_PWM, int(self.dy ** 0.5 )+30)
sleep(0.03)
示例9:
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import analog_write [as 别名]
# show the pygame window
pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("Pygame Example")
# loop around until the user presses escape or Q
looping = True
red = 0
green = 0
blue = 0
while looping:
# fill the screen in the random colour
screen.fill((red, green, blue))
pygame.display.flip()
firmata.analog_write( REDPIN, red )
firmata.analog_write( GREENPIN, green )
firmata.analog_write( BLUEPIN, blue )
# wait for a key to be pressed
key = wait_for_key()
# Test for Xbox Pad Buttons
if key == pygame.K_b:
red=red+8
if red>255:
red=0
if key == pygame.K_a:
green=green+8
if green>255:
示例10: Firmata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import analog_write [as 别名]
class Firmata(Adaptor):
def __init__(self, options):
super(Firmata, self).__init__(options)
if 'port' not in options:
raise self.ParameterRequired(
'A port must be specified for Firmata connection.'
)
self.port = options.get('port')
self.board = PyMata('/dev/ttyACM0', verbose=True)
signal.signal(signal.SIGINT, self.signal_handler)
self.pins = {
'digital': [],
'analog': [],
'pwm': [],
'servo': [],
'i2c': [],
}
def analog_write(self, pin_number, value):
if pin_number not in self.pins['analog']:
self.pins['analog'].append(pin_number)
self.board.set_pin_mode(
pin_number,
self.board.OUTPUT,
self.board.ANALOG
)
self.board.analog_write(pin_number, value)
def analog_read(self, pin_number):
if pin_number not in self.pins['analog']:
self.pins['analog'].append(pin_number)
self.board.set_pin_mode(
pin_number,
self.board.INPUT,
self.board.ANALOG
)
return self.board.analog_read(pin_number)
def digital_write(self, pin_number, value):
if pin_number not in self.pins['digital']:
self.pins['digital'].append(pin_number)
self.board.set_pin_mode(
pin_number,
self.board.OUTPUT,
self.board.DIGITAL
)
self.board.digital_write(pin_number, value)
def digital_read(self, pin_number):
if pin_number not in self.pins['digital']:
self.pins['digital'].append(pin_number)
self.board.set_pin_mode(
pin_number,
self.board.INPUT,
self.board.DIGITAL
)
return self.board.analog_write(pin_number)
def pwm_write(self, pin_number, value):
if pin_number not in self.pins['pwm']:
self.pins['pwm'].append(pin_number)
self.board.set_pin_mode(
pin_number,
self.board.PWM,
self.board.DIGITAL
)
return self.board.analog_write(pin_number, value)
def pwm_read(self, pin_number):
if pin_number not in self.pins['pwm']:
self.pins['pwm'].append(pin_number)
self.board.set_pin_mode(
pin_number,
self.board.PWM,
self.board.DIGITAL
)
return self.board.analog_read(pin_number)
def servo_write(self, pin_number, value):
if pin_number not in self.pins['servo']:
self.pins['servo'].append(pin_number)
self.board.servo_config(pin_number)
self.board.analog_write(pin_number, value)
def disconnect(self):
# Close the firmata interface down cleanly
self.board.close()
#.........这里部分代码省略.........
示例11: brightness
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import analog_write [as 别名]
# Set the pin mode for a digital output pin
firmata.set_pin_mode(RED_LED, firmata.OUTPUT, firmata.DIGITAL)
# Turn on the RED LED
firmata.digital_write(RED_LED, 1)
# Wait 3 seconds and turn it off
time.sleep(3)
firmata.digital_write(RED_LED, 0)
# Set the white led for pwm operation
firmata.set_pin_mode(WHITE_LED, firmata.PWM, firmata.DIGITAL)
# Set the white led to full brightness (255) for 1 second
firmata.analog_write(WHITE_LED, 255)
time.sleep(1)
# now set it to half brightness for 1 second
firmata.analog_write(WHITE_LED, 128)
time.sleep(1)
# and finally extinguish it
firmata.analog_write(WHITE_LED, 0)
# set potentiometer pin as an analog input
firmata.set_pin_mode(POTENTIOMETER, firmata.INPUT, firmata.ANALOG)
# allow some time for the first data to arrive from the Arduino and be
# processed.
time.sleep(.2)
示例12: FSMHand
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import analog_write [as 别名]
class FSMHand():
def __init__(self):
# Initialize fingers and wrist pos
self.finger_pos = [0, 0, 0, 0, 0, 90]
# Initialize sensor values
self.sensor_val = [0, 0, 0, 0, 0]
# Initialize the arduino
self.arduino = PyMata("/dev/ttyACM0", verbose=True)
# Initialize the servos and sensors on arduino
for servo_pin in SERVO_PINS:
self.arduino.servo_config(servo_pin)
for sensor_pin in SENSOR_PINS:
self.arduino.enable_analog_reporting(sensor_pin)
# Initialize the hand states
self.curstate = 'open'
self.states = {}
self.transitionTable = {}
#states are a dictionary of name/function pairints stored in a dictionary
#i.e. {'open':self.Open}
def AddFSMState(self,name,function):
self.states[name] = function
#each state gets its own transition table
#a transition table is a list of states to switch to
#given a "event"
def AddFSMTransition(self,name,transitionDict):
#yes we are making a dictionary the value bound to a dictionary key
self.transitionTable[name] = transitionDict
def Move_callback(self, data):
servo_pose = data.finger_pose
if self.curstate == 'open':
for i, pin in enumerate(FINGER_PINS):
self.arduino.analog_write(pin, servo_pose)
self.finger_pos[i] = servo_pose
rospy.loginfo(rospy.get_caller_id() + " I heard %d", servo_pose)
def Event_handler(self):
curstatefunction = self.states[self.curestate]
curstatefunction()
def RunFSM(self):
pub = rospy.Publisher('finger_status', Pressure, queue_size=10)
rate = rospy.Rate(50)
while not rospy.is_shutdown():
for i, sensor_pin in enumerate(SENSOR_PINS):
self.sensor_val[i] = self.arduino.analog_read(sensor_pin)
outdata = Pressure()
outdata.sensor1 = self.sensor_val[0]
pub.publish(outdata)
if max(self.sensor_val) > 500 or max(self.finger_pos) == 150:
self.curstate = 'close'
else:
self.curstate = 'open'
print "Current State: ", self.curstate
rate.sleep()