Python: setting value of attribute

时间:2016-10-20 13:22:32

标签: python attributes

In this API, it says the following regarding setting a motor's polarity:

polarity

Sets the polarity of the motor. With normal polarity, a positive duty cycle will cause the motor to rotate clockwise. With inversed polarity, a positive duty cycle will cause the motor to rotate counter-clockwise. Valid values are normal and inversed.

import ev3dev.ev3 as ev3
import numpy as numpy
m = ev3.LargeMotor('outA')
time = numpy.random.normal(loc=5.0, scale=1.0, size=None)
time = time * 1000
speed = 180 / ( time / 1000 )
m.run_timed(time_sp=time, speed_sp=speed)
m.polarity = 'inverse'
time = numpy.random.normal(loc=5.0, scale=1.0, size=None)
time = time * 1000
speed = 180 / ( time / 1000 )
m.run_timed(time_sp=5000, speed_sp=100)

m.polarity = 'inverse' doesn't work:

robot@ev3dev:~$ python gaussian_motor.py 
Traceback (most recent call last):
  File "gaussian_motor.py", line 8, in <module>
    m.polarity = 'inverse'
  File "/usr/lib/python2.7/dist-packages/ev3dev/core.py", line 388, in polarity
    self.set_attr_string('polarity', value)
  File "/usr/lib/python2.7/dist-packages/ev3dev/core.py", line 216, in set_attr_string
    self._set_attribute(attribute, "{0}".format(value))
  File "/usr/lib/python2.7/dist-packages/ev3dev/core.py", line 204, in _set_attribute
    self._attribute_cache.write(abspath(self._path + '/' + attribute), value)
  File "/usr/lib/python2.7/dist-packages/ev3dev/core.py", line 91, in write
    f.write(value.encode())
IOError: [Errno 22] Invalid argument

How am I supposed to do this?

1 个答案:

答案 0 :(得分:1)

  

有效值为正常值,反向 d 。 (强调我的)

你忘记了d。

看起来db.createCollection(name:("string")) 是一种设置极性的方法。您可能需要像polarity

那样访问它