Class: Lignite::Motors

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

Overview

Send DirectCommands to motors. It’s called Motors instead of Motor because some methods are designed to work on a pair if motors driving a vehicle.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(layer, nos, dc = Lignite::DirectCommands.new) ⇒ Motors

Returns a new instance of Motors.



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

def initialize(layer, nos, dc = Lignite::DirectCommands.new)
  @layer = layer
  @nos = nos
  @dc = dc
end

Instance Attribute Details

#dcLignite::DirectCommands (readonly)



12
13
14
# File 'lib/lignite/motors.rb', line 12

def dc
  @dc
end

#layerObject (readonly)

do the DCs spawn independent threads?? must run ready in the same block?



9
10
11
# File 'lib/lignite/motors.rb', line 9

def layer
  @layer
end

#nosObject (readonly)

Returns the value of attribute nos.



10
11
12
# File 'lib/lignite/motors.rb', line 10

def nos
  @nos
end

Instance Method Details

#clr_countObject

zero tachos, for use as sensor



152
153
154
# File 'lib/lignite/motors.rb', line 152

def clr_count
  dc.output_clr_count(layer, nos)
end

#get_countObject

rubocop:disable Naming/AccessorMethodName, upstream API name



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/lignite/motors.rb', line 156

def get_count # rubocop:disable Naming/AccessorMethodName, upstream API name
  layer = @layer
  nos_as_indices.map do |no|
    tachos = dc.with_reply do
      data32 :tachos
      block do
        output_get_count(layer, no, :tachos)
      end
    end
    tachos
  end
end

#nos_as_bitsObject

0x02 | 0x04 | 0x08 -> [0x02, 0x04, 0x08]



23
24
25
26
27
28
# File 'lib/lignite/motors.rb', line 23

def nos_as_bits
  r = [1, 2, 4, 8].find_all do |n|
    (nos & n) != 0
  end
  r
end

#nos_as_indicesObject

0x02 | 0x04 | 0x08 -> [1, 2, 3]



15
16
17
18
19
20
# File 'lib/lignite/motors.rb', line 15

def nos_as_indices
  r = [0, 1, 2, 3].find_all do |n|
    (nos & (1 << n)) != 0
  end
  r
end

#polarity(pol) ⇒ Object

ATTR ~polarity



78
79
80
# File 'lib/lignite/motors.rb', line 78

def polarity(pol)
  dc.output_polarity(layer, nos, pol)
end

#power(power) ⇒ Object

ATTR power ~ speed ?



65
66
67
# File 'lib/lignite/motors.rb', line 65

def power(power)
  dc.output_power(layer, nos, power) # does this start them also?
end

#prg_stopObject

WTF?



170
171
172
# File 'lib/lignite/motors.rb', line 170

def prg_stop
  dc.output_prg_stop
end

#readObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/lignite/motors.rb', line 82

def read
  layer = @layer
  nos_as_indices.map do |no|
    speed_tacho_pair = dc.with_reply do
      data32 :tacho
      data8 :speed
      block do
        output_read(layer, no, :speed, :tacho)
      end
    end
    speed_tacho_pair
  end
end

#readyObject

which commands are affected? not output_start they say



111
112
113
# File 'lib/lignite/motors.rb', line 111

def ready
  dc.output_ready(layer, nos)
end

#resetObject

ATTR tacho, like degrees



55
56
57
# File 'lib/lignite/motors.rb', line 55

def reset
  dc.output_reset(lay, nos) # tacho counts
end

#set_typeObject

the type is an OUT param so the VM SETs and we GET to learn the type?



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

def set_type
  layer = @layer
  nos_as_indices.map do |no|
    type = dc.with_reply do
      data8 :type
      block do
        output_set_type(layer, no, :type)
      end
    end
    type
  end
end

#speed(speed) ⇒ Object



69
70
71
# File 'lib/lignite/motors.rb', line 69

def speed(speed)
  dc.output_speed(layer, nos, speed) # does this start them also?
end

#startObject



73
74
75
# File 'lib/lignite/motors.rb', line 73

def start
  dc.output_start(layer, nos) # apparently not
end

#step_power(power, deg1, deg2, deg3, brake = Lignite::BRAKE) ⇒ Object

TODO better param protocol?

Parameters:

  • power (Integer)

    -100..100



117
118
119
# File 'lib/lignite/motors.rb', line 117

def step_power(power, deg1, deg2, deg3, brake = Lignite::BRAKE)
  dc.output_step_power(layer, nos, power, deg1, deg2, deg3, brake)
end

#step_speed(speed, deg1, deg2, deg3, brake = Lignite::BRAKE) ⇒ Object

tachos

Parameters:

  • speed (Integer)

    -100..100



129
130
131
# File 'lib/lignite/motors.rb', line 129

def step_speed(speed, deg1, deg2, deg3, brake = Lignite::BRAKE)
  dc.output_step_speed(layer, nos, speed, deg1, deg2, deg3, brake)
end

#step_sync(speed, turn, tachos, brake = Lignite::BRAKE) ⇒ Object

Parameters:

  • speed (Integer)

    -100..100

  • turn (Integer)

    -200..200: 0 straight, 100 stops the right motor, -100 stops the left motor > 100 reverses the right motor, < -100 reverses the left motor



143
144
145
# File 'lib/lignite/motors.rb', line 143

def step_sync(speed, turn, tachos, brake = Lignite::BRAKE)
  dc.output_step_sync(layer, nos, speed, turn, tachos, brake)
end

#stop(brake = Lignite::BRAKE) ⇒ Object

ATTR running



60
61
62
# File 'lib/lignite/motors.rb', line 60

def stop(brake = Lignite::BRAKE)
  dc.output_stop(layer, nos, brake)
end

#testBoolean

ATTR running?

Returns:

  • (Boolean)

    true if busy/running



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/lignite/motors.rb', line 98

def test
  layer = @layer
  nos = @nos
  busy = dc.with_reply do
    data8 :busy
    block do
      output_test(layer, nos, :busy)
    end
  end
  busy != 0
end

#time_power(power, ms1, ms2, ms3, brake = Lignite::BRAKE) ⇒ Object

msec

Parameters:

  • power (Integer)

    -100..100



123
124
125
# File 'lib/lignite/motors.rb', line 123

def time_power(power, ms1, ms2, ms3, brake = Lignite::BRAKE)
  dc.output_time_power(layer, nos, power, ms1, ms2, ms3, brake)
end

#time_speed(speed, ms1, ms2, ms3, brake = Lignite::BRAKE) ⇒ Object

msec

Parameters:

  • speed (Integer)

    -100..100



135
136
137
# File 'lib/lignite/motors.rb', line 135

def time_speed(speed, ms1, ms2, ms3, brake = Lignite::BRAKE)
  dc.output_time_speed(layer, nos, speed, ms1, ms2, ms3, brake)
end

#time_sync(speed, turn, ms, brake = Lignite::BRAKE) ⇒ Object



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

def time_sync(speed, turn, ms, brake = Lignite::BRAKE)
  dc.output_time_sync(layer, nos, speed, turn, ms, brake)
end