Skip to content

Commit

Permalink
execution (partial)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcloutier committed Dec 6, 2015
1 parent c5c8db2 commit 5efdd1f
Show file tree
Hide file tree
Showing 21 changed files with 400 additions and 96 deletions.
9 changes: 0 additions & 9 deletions lib/ev3/body/color_sensor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,4 @@ defmodule Ev3.ColorSensor do
LegoSensor.set_mode(sensor, @color)
end

# Give currently set mode
defp mode(sensor) do
case LegoSensor.mode(sensor) do
@reflect -> :reflect
@ambient -> :ambient
@color -> :color
end
end

end
9 changes: 0 additions & 9 deletions lib/ev3/body/infrared_sensor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,4 @@ defmodule Ev3.InfraredSensor do
LegoSensor.set_mode(sensor, @remote)
end

# Give currently set mode
defp mode(sensor) do
case LegoSensor.mode(sensor) do
@proximity -> :proximity
@seek -> :seek
@remote -> :remote
end
end

end
16 changes: 10 additions & 6 deletions lib/ev3/body/lego_motor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ defmodule Ev3.LegoMotor do
|> Enum.filter(&(String.starts_with?(&1, @prefix)))
|> Enum.map(&(init_motor("#{@sys_path}/#{&1}")))
else
[Ev3.Mock.Tachomotor.new(:large), Ev3.Mock.Tachomotor.new(:medium)]
[Ev3.Mock.Tachomotor.new(:large, "A"), Ev3.Mock.Tachomotor.new(:medium, "B")]
end
end

defp dispatch(motor) do
defp module_for(_motor) do
if !Ev3.testing?() do
Ev3.Tachomotor
else
Expand All @@ -30,14 +30,14 @@ defmodule Ev3.LegoMotor do

@doc "Get the list of senses from a motor"
def senses(motor) do
apply(dispatch(motor), :senses, [motor])
apply(module_for(motor), :senses, [motor])
end


@doc "Read the value of a sense from a motor"
def read(motor, sense) do # {value, updated_motor} - value can be nil
try do
apply(dispatch(motor), :read, [motor, sense])
apply(module_for(motor), :read, [motor, sense])
rescue
error ->
Logger.warn("#{inspect error} when reading #{inspect sense} from #{inspect motor}")
Expand All @@ -47,12 +47,12 @@ defmodule Ev3.LegoMotor do

@doc "Get how long to pause between reading a sense from a motor. In msecs"
def pause(motor) do
apply(dispatch(motor), :pause, [motor])
apply(module_for(motor), :pause, [motor])
end

@doc "Get the resolution of a motor (the delta between essentially identical readings). Nil or an integer."
def sensitivity(motor, sense) do
apply(dispatch(motor), :sensitivity, [motor, sense])
apply(module_for(motor), :sensitivity, [motor, sense])
end

@doc "Is this a large motor?"
Expand All @@ -65,6 +65,10 @@ defmodule Ev3.LegoMotor do
motor.type == :medium
end

def execute_command(motor, command, params) do
apply(module_for(motor), command, [motor | params])
end

### PRIVATE

defp init_motor(path) do
Expand Down
10 changes: 5 additions & 5 deletions lib/ev3/body/lego_sensor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Ev3.LegoSensor do
end
end

defp dispatch(sensor) do
defp module_for(sensor) do
if !Ev3.testing?() do
case sensor.type do
:touch -> Ev3.TouchSensor
Expand All @@ -39,14 +39,14 @@ defmodule Ev3.LegoSensor do

@doc "Get the list of senses from a sensor"
def senses(sensor) do
apply(dispatch(sensor), :senses, [sensor])
apply(module_for(sensor), :senses, [sensor])
end


@doc "Read the value of a sense from a sensor"
def read(sensor, sense) do # {value, updated_sensor} - value can be nil
try do
apply(dispatch(sensor), :read, [sensor, sense])
apply(module_for(sensor), :read, [sensor, sense])
rescue
error ->
Logger.warn("#{inspect error} when reading #{inspect sense} from #{inspect sensor}")
Expand All @@ -56,12 +56,12 @@ defmodule Ev3.LegoSensor do

@doc "Get how long to pause between reading a sense from a sensor. In msecs"
def pause(sensor) do
apply(dispatch(sensor), :pause, [sensor])
apply(module_for(sensor), :pause, [sensor])
end

@doc "Get the resolution of a sensor (the delta between essentially identical readings). Nil or an integer."
def sensitivity(sensor, sense) do
apply(dispatch(sensor), :sensitivity, [sensor, sense])
apply(module_for(sensor), :sensitivity, [sensor, sense])
end

@doc "Is this the ultrasonic sensor?"
Expand Down
78 changes: 74 additions & 4 deletions lib/ev3/body/mock/mock_tacho_motor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ defmodule Ev3.Mock.Tachomotor do
@moduledoc "A mock large tachomotor"

@behaviour Ev3.Sensing
@behaviour Ev3.Moving

def new(type) do
%Ev3.Device{class: :motor,
path: "/mock/#{type}_tachomotor",
type: type}
def new(type, port_name) do
%Ev3.Device{class: :motor,
path: "/mock/#{type}_tachomotor",
type: type,
port: port_name}
end

# Sensing
Expand All @@ -32,6 +34,74 @@ defmodule Ev3.Mock.Tachomotor do
nil
end

# Moving

def reset(motor) do
IO.puts("Resetting #{motor.path}")
motor
end

def set_speed(motor, mode, speed) do
IO.puts("Setting the speed of #{motor.path} to #{speed} #{mode}")
motor
end

def reverse_polarity(motor) do
IO.puts("Reversing polarity of #{motor.path}")
motor
end

def set_duty_cycle(motor, duty_cycle) do
IO.puts("Setting the duty cycle of #{motor.path} to #{duty_cycle}")
motor
end

def set_ramp_up(motor, msecs) do
IO.puts("Setting ramp-up of #{motor.path} to #{msecs} msecs")
motor
end

def set_ramp_down(motor, msecs) do
IO.puts("Setting ramp-up of #{motor.path} to #{msecs} msecs")
motor
end

def run(motor) do
IO.puts("Running #{motor.path}")
motor
end

def run_to_absolute(motor, degrees) do
IO.puts("Running #{motor.path} to #{degrees} absolute degrees")
motor
end

def run_to_relative(motor, degrees) do
IO.puts("Running #{motor.path} to #{degrees} relative degrees")
motor
end

def run_for(motor, msecs) when is_integer(msecs) do
IO.puts("Running #{motor.path} for #{msecs} msecs")
:timer.sleep(msecs)
motor
end

def coast(motor) do
IO.puts("Coasting #{motor.path}")
motor
end

def brake(motor) do
IO.puts("Braking #{motor.path}")
motor
end

def hold(motor) do
IO.puts("Holding #{motor.path}")
motor
end

### PRIVATE

defp current_speed(motor) do
Expand Down
2 changes: 1 addition & 1 deletion lib/ev3/body/mock/mock_touch_sensor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Ev3.Mock.TouchSensor do
[:touch]
end

def read(sensor, sense) do
def read(sensor, _sense) do
value = case :random.uniform(2) - 1 do
0 -> :released
1 -> :pressed
Expand Down
31 changes: 31 additions & 0 deletions lib/ev3/body/moving.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Ev3.Moving do
use Behaviour


defcallback reset(motor :: %Ev3.Device{}) :: %Ev3.Device{}

defcallback set_speed(motor :: %Ev3.Device{}, mode :: atom, speed :: number) :: %Ev3.Device{}

defcallback reverse_polarity(motor :: %Ev3.Device{}) :: %Ev3.Device{}

defcallback set_duty_cycle(motor :: %Ev3.Device{}, duty_cycle :: number) :: %Ev3.Device{}

defcallback run(motor :: %Ev3.Device{}) :: %Ev3.Device{}

defcallback run_for(motor :: %Ev3.Device{}, duration :: number) :: %Ev3.Device{}

defcallback run_to_absolute(motor :: %Ev3.Device{}, degrees :: number) :: %Ev3.Device{}

defcallback run_to_relative(motor :: %Ev3.Device{}, degrees :: number) :: %Ev3.Device{}

defcallback coast(motor :: %Ev3.Device{}) :: %Ev3.Device{}

defcallback brake(motor :: %Ev3.Device{}) :: %Ev3.Device{}

defcallback hold(motor :: %Ev3.Device{}) :: %Ev3.Device{}

defcallback set_ramp_up(motor :: %Ev3.Device{}, msecs :: number) :: %Ev3.Device{}

defcallback set_ramp_down(motor :: %Ev3.Device{}, msecs :: number) :: %Ev3.Device{}

end
49 changes: 28 additions & 21 deletions lib/ev3/body/tachomotor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Ev3.Tachomotor do
@moduledoc "A tacho motor"

@behaviour Ev3.Sensing
@behaviour Ev3.Moving

alias Ev3.Device
alias Ev3.LegoMotor
Expand Down Expand Up @@ -36,7 +37,7 @@ defmodule Ev3.Tachomotor do
end
end

####
#### Acting

@doc "Reset a motor to defaul control values"
def reset(motor) do
Expand All @@ -60,17 +61,21 @@ defmodule Ev3.Tachomotor do
motor1 = set_control(motor, :time, msecs)
apply_motor_controls(motor1)
execute_command(motor1, "run-timed")
:timer.sleep(msecs)
motor1
end

@doc "Run a motor to an absolute position in degrees according to controls"
def run_to_absolute(motor, degrees) do
run_to_position(motor, :absolute, degrees)
# TODO wait until absolute position reached, or time out
motor
end

@doc "Run a motor to a relative position in degrees according to controls"
def run_to_relative(motor, degrees) do
run_to_position(motor, :relative, degrees)
# TODO wait until relative position reached, or time out
motor
end

Expand Down Expand Up @@ -109,11 +114,6 @@ defmodule Ev3.Tachomotor do
end
end

@doc "Get current polarity"
def polarity(motor) do
get_control(motor, :polarity)
end

@doc "Change the motor's duty cycle. Immediately effective only when running direct"
def set_duty_cycle(motor, value) when value in -100 .. 100 do
set_attribute(motor, "duty_cycle_sp", value) #takes effect immediately
Expand All @@ -122,11 +122,6 @@ defmodule Ev3.Tachomotor do
|> set_control(:speed, 0)
end

@doc "Get target duty cycle"
def target_duty_cycle(motor) do
get_control(motor, :duty_cycle)
end

@doc "Set the target speed to maintain in degrees per second"
def set_speed(motor, :dps, value) when is_number(value) do
count_per_sec = round(value * count_per_rot(motor) / 360)
Expand All @@ -145,16 +140,6 @@ defmodule Ev3.Tachomotor do
|> set_control(:speed, count_per_sec)
end

@doc "Get the target speed in rotations per second"
def target_speed(motor, :rps) do # rotations per second
get_control(motor, :speed) / count_per_rot(motor)
end

@doc "Get the target speed in degrees per second"
def target_speed(motor, :dps) do # degrees per second
get_control(motor, :speed) * (360 / count_per_rot(motor))
end

@doc "Set the target position in degrees"
def set_target_degrees(motor, degrees) when is_number(degrees) do
count = round(degrees * count_per_rot(motor) / 360)
Expand Down Expand Up @@ -187,6 +172,28 @@ defmodule Ev3.Tachomotor do
set_control(motor, :ramp_down, msecs)
end

###########

@doc "Get target duty cycle"
def target_duty_cycle(motor) do
get_control(motor, :duty_cycle)
end

@doc "Get the target speed in rotations per second"
def target_speed(motor, :rps) do # rotations per second
get_control(motor, :speed) / count_per_rot(motor)
end

@doc "Get the target speed in degrees per second"
def target_speed(motor, :dps) do # degrees per second
get_control(motor, :speed) * (360 / count_per_rot(motor))
end

@doc "Get current polarity"
def polarity(motor) do
get_control(motor, :polarity)
end

@doc "Get the actual motor speed"
def current_speed(motor, mode) do
speed = get_attribute(motor, "speed", :integer)
Expand Down
Loading

0 comments on commit 5efdd1f

Please sign in to comment.