dc.py
1 # -*- coding: utf-8 -*-
2 
3 from time import sleep
4 import sys
5 from ev3dev import *
6 
7 if __name__ == '__main__':
8  print 'Waiting the EV3 brick online...'
9  if ev3_init() < 1: sys.exit( 1 )
10 
11  print '*** ( EV3 ) Hello! ***'
13  port = OUTPUT_B
14 
15  name_port = ev3_port_name( port, EXT_PORT__NONE_, 0 )
16  print 'Set mode of the EV3 output port (%s)...' % ( name_port )
17  sn_port = ev3_search_port( port, EXT_PORT__NONE_ )
18  set_port_mode_inx( sn_port, OUTPUT_DC_MOTOR )
19  ok, mode = get_port_mode( sn_port, 256 )
20  if ok:
21  print '%s: %s' % ( name_port, mode )
22  sleep( 0.2 )
23 
24  ev3_dc_init()
25 
26  print 'Found DC motors:'
27  for i in range( DC_DESC__LIMIT_ ):
28  type_inx = ev3_dc_desc_type_inx( i )
29  if type_inx != DC_TYPE__NONE_:
30  print ' type =', ev3_dc_type( type_inx )
31  print ' port =', ev3_dc_port_name( i )
32 
33  ok, sn = ev3_search_dc_plugged_in( port, EXT_PORT__NONE_ )
34  if ok:
35  print 'DC motor is found, run for 5 sec...'
36  set_dc_ramp_up_sp( sn, 2000 )
37  set_dc_duty_cycle_sp( sn, 100 )
38  set_dc_stop_action_inx( sn, DC_COAST )
39  set_dc_command_inx( sn, DC_RUN_FOREVER )
40  ok, state = get_dc_state( sn, 256 )
41  if ok:
42  print 'state:', state
43  sleep( 5.0 )
44  set_dc_command_inx( sn, DC_STOP )
45  ok, state = get_dc_state( sn, 256 )
46  if ok:
47  print 'state:', state
48  else:
49  print 'DC motor is NOT found'
50 
51  sleep( 0.2 )
52  print 'Reset mode of the EV3 output port...'
53  set_port_mode_inx( sn_port, OUTPUT_AUTO )
54  ok, mode = get_port_mode( sn_port, 256 )
55  if ok:
56  print '%s: %s' % ( name_port, mode )
57 
58  ev3_uninit()
59  print '*** ( EV3 ) Bye! ***'