Class: LegoEv3::TachoMotor

Inherits:
Object
  • Object
show all
Defined in:
lib/tacho_motor.rb

Overview

More info: www.ev3dev.org/docs/drivers/tacho-motor-class/ TODO: ramp_up_sp, ramp_down_sp TODO: handle ‘run_direct’ and ‘run_forever’ states TODO: speed_regulation by default?

Instance Method Summary collapse

Constructor Details

#initialize(connection, id, port) ⇒ TachoMotor

Returns a new instance of TachoMotor.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tacho_motor.rb', line 7

def initialize(connection, id, port)
  @connection = connection
  @id = id
  @port = port

  @speed = 0
  @desired_speed = 0
  @ticks_per_rotation = 0
  @polarity = :normal
  @position = 0
  @desired_position = 0
  @desired_time_ms = 0
  @stop_mode = :coast
  @running = false
  @ramping = false
  @holding = false
  @stalled = false
  @regulated_speed = false

  sync!
end

Instance Method Details

#desired_positionObject



147
148
149
# File 'lib/tacho_motor.rb', line 147

def desired_position
  @desired_position = LegoEv3::Commands::TachoMotor.get_position_sp!(@connection, @id)
end

#desired_speedObject



121
122
123
# File 'lib/tacho_motor.rb', line 121

def desired_speed
  @desired_speed = LegoEv3::Commands::TachoMotor.get_duty_cycle_sp!(@connection, @id)
end

#desired_timeObject



151
152
153
# File 'lib/tacho_motor.rb', line 151

def desired_time
  @desired_time = LegoEv3::Commands::TachoMotor.get_time_sp!(@connection, @id)
end

#holdingObject



182
183
184
185
# File 'lib/tacho_motor.rb', line 182

def holding
  update_states
  @holding
end

#infoObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/tacho_motor.rb', line 215

def info
  {
    id: @id,
    port: @port,
    ticks_per_rotation: @ticks_per_rotation,
    speed: @speed,
    desired_speed: @desired_speed,
    position: @position,
    desired_position: @desired_position,
    polarity: @polarity,
    desired_time_ms: @desired_time_ms,
    stop_mode: @stop_mode,
    running: @running,
    ramping: @ramping,
    holding: @holding,
    stalled: @stalled
  }
end

#polarityObject



125
126
127
# File 'lib/tacho_motor.rb', line 125

def polarity
  @polarity = LegoEv3::Commands::TachoMotor.get_polarity!(@connection, @id)
end

#polarity=(new_value) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/tacho_motor.rb', line 129

def polarity=(new_value)
  unless [:normal, :inversed].include?(new_value.to_sym)
    raise Exception.new('Invalid polarity. Possible values: :normal, :inversed.')
  end

  LegoEv3::Commands::TachoMotor.set_polarity!(@connection, @id, new_value)
  polarity
end

#positionObject



138
139
140
# File 'lib/tacho_motor.rb', line 138

def position
  @position = LegoEv3::Commands::TachoMotor.get_position!(@connection, @id)
end

#position=(new_value) ⇒ Object



142
143
144
145
# File 'lib/tacho_motor.rb', line 142

def position=(new_value)
  LegoEv3::Commands::TachoMotor.set_position!(@connection, @id, new_value.to_i)
  position
end

#rampingObject



177
178
179
180
# File 'lib/tacho_motor.rb', line 177

def ramping
  update_states
  @ramping
end

#regulated_speedObject



192
193
194
# File 'lib/tacho_motor.rb', line 192

def regulated_speed
  @regulated_speed = LegoEv3::Commands::TachoMotor.get_speed_regulation!(@connection, @id)
end

#regulated_speed=(new_value) ⇒ Object



196
197
198
199
# File 'lib/tacho_motor.rb', line 196

def regulated_speed=(new_value)
  LegoEv3::Commands::TachoMotor.set_speed_regulation!(@connection, @id, new_value.kind_of?(TrueClass) ? 'on' : 'off')
  regulated_speed
end

#resetObject



96
97
98
99
# File 'lib/tacho_motor.rb', line 96

def reset
  LegoEv3::Commands::TachoMotor.reset!(@connection, @id)
  sync!
end

#run_directObject

Run the motor at the desired_speed. Unlike other run commands, changing desired_speed take immediate effect.



88
89
90
# File 'lib/tacho_motor.rb', line 88

def run_direct
  LegoEv3::Commands::TachoMotor.run_direct!(@connection, @id)
end

#run_foreverObject

Run the motor indefinitely, until another command is sent.



30
31
32
33
34
# File 'lib/tacho_motor.rb', line 30

def run_forever
  ensure_valid_speed

  LegoEv3::Commands::TachoMotor.run_forever!(@connection, @id)
end

#run_timed(desired_time_ms) ⇒ Object

Run the motor for the amount of time specified in desired_time_ms. Then stop using the current stop behavior.



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/tacho_motor.rb', line 72

def run_timed(desired_time_ms)
  ensure_valid_speed
  old_position = position

  LegoEv3::Commands::TachoMotor.set_time_sp!(@connection, @id, desired_time_ms.to_i)
  LegoEv3::Commands::TachoMotor.run_timed!(@connection, @id)

  loop do
    break if operation_completed?(old_position)
  end

  position
end

#run_to_absolute_position(desired_position) ⇒ Object

Run to an absolute position specified by desired_position. Then stop using the current stop behavior.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tacho_motor.rb', line 38

def run_to_absolute_position(desired_position)
  ensure_valid_speed
  old_position = position

  LegoEv3::Commands::TachoMotor.set_position_sp!(@connection, @id, desired_position)
  LegoEv3::Commands::TachoMotor.run_to_abs_pos!(@connection, @id)

  loop do
    break if operation_completed?(old_position)
  end

  position
end

#run_to_relative_position(desired_position) ⇒ Object

Run to a position relative to the current position. The new position will be position + desired_position. Then stop using the current stop behavior.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tacho_motor.rb', line 55

def run_to_relative_position(desired_position)
  ensure_valid_speed
  old_position = position

  LegoEv3::Commands::TachoMotor.set_position_sp!(@connection, @id, old_position + desired_position)
  LegoEv3::Commands::TachoMotor.run_to_abs_pos!(@connection, @id)

  loop do
    ap info
    break if operation_completed?(old_position)
  end

  position
end

#runningObject



172
173
174
175
# File 'lib/tacho_motor.rb', line 172

def running
  update_states
  @running
end

#speedObject



105
106
107
# File 'lib/tacho_motor.rb', line 105

def speed
  @speed = LegoEv3::Commands::TachoMotor.get_duty_cycle!(@connection, @id)
end

#speed=(new_value) ⇒ Object

This is actually the desired speed but feels more natural.



110
111
112
113
114
115
116
117
118
119
# File 'lib/tacho_motor.rb', line 110

def speed=(new_value)
  sanitized = [[new_value.to_i, -100].max, 100].min

  # I will probably learn why this is not right but for now,
  # I sync those 2 values to keep it simple.
  LegoEv3::Commands::TachoMotor.set_speed_sp!(@connection, @id, sanitized)
  LegoEv3::Commands::TachoMotor.set_duty_cycle_sp!(@connection, @id, sanitized)

  desired_speed
end

#stalledObject



187
188
189
190
# File 'lib/tacho_motor.rb', line 187

def stalled
  update_states
  @stalled
end

#statesObject



168
169
170
# File 'lib/tacho_motor.rb', line 168

def states
  @states = LegoEv3::Commands::TachoMotor.get_states!(@connection, @id)
end

#stopObject



92
93
94
# File 'lib/tacho_motor.rb', line 92

def stop
  LegoEv3::Commands::TachoMotor.stop!(@connection, @id)
end

#stop_modeObject



155
156
157
# File 'lib/tacho_motor.rb', line 155

def stop_mode
  @stop_mode = LegoEv3::Commands::TachoMotor.get_stop_command!(@connection, @id)
end

#stop_mode=(new_value) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/tacho_motor.rb', line 159

def stop_mode=(new_value)
  unless [:coast, :brake, :hold].include?(new_value.to_sym)
    raise Exception.new('Invalid stop behavior. Possible values: :coast, :brake, :hold.')
  end

  LegoEv3::Commands::TachoMotor.set_stop_command!(@connection, @id, new_value)
  stop_mode
end

#sync!Object



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/tacho_motor.rb', line 201

def sync!
  ticks_per_rotation
  speed
  desired_speed
  polarity
  position
  desired_position
  desired_time
  stop_mode
  update_states

  info
end

#ticks_per_rotationObject



101
102
103
# File 'lib/tacho_motor.rb', line 101

def ticks_per_rotation
  @ticks_per_rotation = LegoEv3::Commands::TachoMotor.get_count_per_rot!(@connection, @id)
end