/** NXT Mindstroms - Sensomoto. @author Piotr Hołownia */ :- module(nxt_sensomoto,[ nxt_connection_open/0, nxt_connection_close/0, nxt_motor/2, nxt_motor/3, nxt_touch_sensor/2, nxt_sound_sensor/2, nxt_light_sensor/2, nxt_light_sensor_LED/2, nxt_ultrasonic_sensor/2 ]). :- use_module(nxt_actions_icommand). :- use_module(threads). %% nxt_open_connection % % Opens connection. nxt_connection_open :- nxt_actions_connection_open. %% nxt_close_connection % % Closes connection. nxt_connection_close :- nxt_actions_connection_close. %% motor(+Motor,+Speed). %% motor(+Motor,-Speed). % % Reads speed of the specified motor % or rotates it at specified speed. nxt_motor(_,Speed) :- nonvar(Speed), Speed > 900, writeln('Rotational speed is to high!'). nxt_motor(_,Speed) :- nonvar(Speed), Speed < -900, writeln('Rotational speed is to high!'). nxt_motor(Motor,Speed) :- nonvar(Motor),nonvar(Speed), Speed > 0, nxt_actions_motor_forward(Motor,Speed). nxt_motor(Motor,Speed) :- nonvar(Motor),nonvar(Speed), Speed < 0, BackwardSpeed is -Speed, nxt_actions_motor_backward(Motor,BackwardSpeed). nxt_motor(Motor,Speed) :- % Returns absolute value of speed. nonvar(Motor),var(Speed), nxt_actions_motor_get_speed(Motor,Speed). nxt_motor(Motor,0) :- nonvar(Motor), nxt_actions_motor_stop(Motor). %% nxt_motor(+Motor,+Speed,time(+Time)). % % Changes motor speed for a specified time. nxt_motor(_,Speed,_) :- nonvar(Speed), Speed > 900, writeln('Rotational speed is to high!'). nxt_motor(_,Speed,_) :- nonvar(Speed), Speed < -900, writeln('Rotational speed is to high!'). nxt_motor(Motor,Speed,time(Time)) :- nonvar(Motor),nonvar(Speed),nonvar(Time), nxt_motor(Motor,OldSpeed), nxt_motor(Motor,Speed), timer_create(_,Time,nxt_motor(Motor,OldSpeed)). %% nxt_motor(+Motor,+Speed,angle(+Angle). %% nxt_motor(+Motor,+Speed,+Angle). % % Rotates motor at specified speed. % Forward if Speed is positive, backward if Speed is negative. % Motor will stop, when specified revolution (Angle in degrees) is reached. nxt_motor(Motor,Speed,angle(Angle)) :- nonvar(Motor),nonvar(Speed),nonvar(Angle), Speed > 0, nxt_actions_motor_rotate(Motor,Speed,Angle). nxt_motor(Motor,Speed,angle(Angle)) :- nonvar(Motor),nonvar(Speed),nonvar(Angle), Speed < 0, MinusSpeed is -Speed, MinusAngle is -Angle, nxt_actions_motor_rotate(Motor,MinusSpeed,MinusAngle). nxt_motor(Motor,Speed,Angle) :- nxt_motor(Motor,Speed,angle(Angle)). %% nxt_touch_sensor(+Port,-Value). % % Gets touch sensor reading. % Returns 1 if pressed, 0 otherwise nxt_touch_sensor(Port,Value) :- nonvar(Port), nxt_actions_touch_sensor(Port,Value). %% nxt_sound_sensor(+Port,-Value). % % Gets sound sensor reading. nxt_sound_sensor(Port,Value) :- nonvar(Port), nxt_actions_sound_sensor(Port,Value). %% nxt_light_sensor(+Port,-Value). % % Gets light sensor reading. nxt_light_sensor(Port,Value) :- nonvar(Port), nxt_actions_light_sensor(Port,Value). %% nxt_light_sensor_LED(+Port,Setting). % % Sets the LED on if Setting is activate or off if passivate. nxt_light_sensor_LED(Port,Setting) :- nonvar(Port), nxt_actions_light_sensor_LED(Port,Setting). %% nxt_ultrasonic_sensor(+Port,-Value). % % Gets ultrasonic sensor reading. nxt_ultrasonic_sensor(Port,Value) :- nonvar(Port), nxt_actions_ultrasonic_sensor(Port,Value).