Skip to content

control dc-motor in python #318

Closed
Closed

Description

@flynder

ev3dev version: Linux ev3dev 4.4.24-16-ev3dev-ev3 #1 PREEMPT Sat Oct 15 11:14:54 CDT 2016 armv5tejl GNU/Linux
ev3dev-lang-python version: 3.4.2.1

I have connected a 9v train track via the ev3->old 9v converter cable in outport port A and can succesfully control the train from shell

echo dc-motor > /sys/devices/platform/legoev3-ports/lego-port/port4/mode
echo 80 > /sys/class/dc-motor/motor0/duty_cycle_sp
echo run-timed > /sys/class/dc-motor/motor0/command
echo 20000 > /sys/class/dc-motor/motor0/time_sp

I have tried to create a python program to do the same but I get file open error

#!/usr/bin/env python3
from ev3dev.ev3 import *
import ev3dev.ev3 as ev3

p = ev3.LegoPort('outA')
assert p.connected
p.mode = 'dc-motor'
m = ev3.DcMotor('outA')
m.run_direct(duty_cycle_sp=75)

it fails with
robot@ev3dev:~$ python3 train.py
Traceback (most recent call last):
File "train.py", line 9, in <module>
m.run_direct(duty_cycle_sp=75)
File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 1302, in run_direct
setattr(self, key, kwargs[key])
File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 1154, in duty_cycle_sp
self._duty_cycle_sp = self.set_attr_int(self._duty_cycle_sp, 'duty_cycle_sp', value)
File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 217, in set_attr_int
return self._set_attribute(attribute, name, str(int(value)))
File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 206, in _set_attribute
attribute.write(value.encode())
io.UnsupportedOperation: File not open for writing
what am I doing wrong?

Activity

  1. ddemidov commented on Apr 3, 2017

    @ddemidov
    Member

    What user are you doing this under? Also, could you do ls -l /sys/class/dc-motor/motor0/ after the python script fails? It looks like the duty_cycle_sp attribute is opened for reading, which means that group 'w' bit is not set (the file is opened here).

  2. ddemidov commented on Apr 3, 2017

    @ddemidov
    Member

    Another thought: can you try to add time.sleep(500) after p.mode = 'dc-motor'? Its possible that the duty_cycle_sp attribute is accessed before its permissions were fully set.

  3. flynder commented on Apr 3, 2017

    @flynder
    Author

    it works! after googling time.sleep and seeing that it is defined in seconds ;)

    a sleep(0.5) is enough to get it started, thx a lot

  4. ddemidov commented on Apr 3, 2017

    @ddemidov
    Member

    after googling time.sleep and seeing that it is defined in seconds ;)

    Ah, sorry about that. I hope you decided to google before it was finished :).

  5. flynder commented on Apr 3, 2017

    @flynder
    Author

    yes, didnt have patience for an 8 minute wait

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions