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.



21
22
23
24
25
# File 'lib/lignite/motors.rb', line 21

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



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

def clr_count
  dc.output_clr_count(layer, nos)
end

#get_countObject

rubocop:disable Naming/AccessorMethodName, upstream API name



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/lignite/motors.rb', line 147

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_indicesObject

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



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

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

#polarity(pol) ⇒ Object

ATTR ~polarity



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

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

#power(power) ⇒ Object

ATTR power ~ speed ?



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

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

#prg_stopObject

WTF?



161
162
163
# File 'lib/lignite/motors.rb', line 161

def prg_stop
  dc.output_prg_stop
end

#readObject



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

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



102
103
104
# File 'lib/lignite/motors.rb', line 102

def ready
  dc.output_ready(layer, nos)
end

#resetObject

ATTR tacho, like degrees



46
47
48
# File 'lib/lignite/motors.rb', line 46

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?



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lignite/motors.rb', line 30

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



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

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

#startObject



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

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



108
109
110
# File 'lib/lignite/motors.rb', line 108

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



120
121
122
# File 'lib/lignite/motors.rb', line 120

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



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

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



51
52
53
# File 'lib/lignite/motors.rb', line 51

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

#testObject

ATTR running?



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lignite/motors.rb', line 88

def test
  layer = @layer
  nos_as_indices.map do |no|
    busy = dc.with_reply do
      data8 :busy
      block do
        output_test(layer, no, :busy)
      end
    end
    busy
  end
end

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

msec

Parameters:

  • power (Integer)

    -100..100



114
115
116
# File 'lib/lignite/motors.rb', line 114

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



126
127
128
# File 'lib/lignite/motors.rb', line 126

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



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

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