%
% Implementation by Marcin Ziółkowski
%
% Copyright (C) 2006-9 by the HeKatE Project
%
% PlNXT has been develped by the HeKatE Project,
% see http://hekate.ia.agh.edu.pl
%
% This file is part of PlNXT.
%
% PlNXT is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% PlNXT is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with PlNXT. If not, see .
%
:- module(nxt_actions_sockets,[
nxt_actions_connection_open/0,
nxt_actions_connection_close/0,
nxt_actions_motor_forward/2,
nxt_actions_motor_backward/2,
nxt_actions_motor_get_speed/2,
nxt_actions_motor_stop/1,
nxt_actions_motor_rotate/3,
nxt_actions_touch_sensor/2,
nxt_actions_sound_sensor/2,
nxt_actions_light_sensor/2,
nxt_actions_light_sensor_LED/2,
nxt_actions_ultrasonic_sensor/2
]).
/** NXT Mindstroms - Actions Sockets.
@authors: API: Piotr Hołownia
Definitions: Marcin Ziółkowski
Motors: 'A', 'B', 'C'.
Sensors ports: 'S1', 'S2', 'S3', 'S4'.
*/
:- use_module(library(socket)).
:- dynamic nxt_actions_sockets/2.
%% nxt_actions_open_connection
%
% Opens connection.
nxt_actions_connection_open :-
tcp_socket(Socket),
tcp_connect(Socket, '127.0.0.1':2056),
tcp_open_socket(Socket, ReadFd, WriteFd),
assert(nxt_actions_sockets(ReadFd, WriteFd)),
write_canonical(WriteFd, 'nxt; open'),
flush_output(WriteFd),
read_line_to_codes(ReadFd, _),
write_canonical(WriteFd, 'shortrep'),
flush_output(WriteFd).
%% nxt_actions_close_connection
%
% Closes connection.
nxt_actions_connection_close :-
nxt_actions_sockets(ReadFd, WriteFd),
write_canonical(WriteFd, 'nxt; close'),
flush_output(WriteFd),
read_line_to_codes(ReadFd, _),
write_canonical(WriteFd, 'bye'),
flush_output(WriteFd),
close(ReadFd),
close(WriteFd),
retractall(nxt_actions_sockets(_,_)).
%% nxt_actions_motor_forward(+Motor,+RotSpeed).
%
% Rotates motor forward at specified speed.
nxt_actions_motor_forward(Motor,RotSpeed) :-
Speed is RotSpeed / 9,
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('motor; setForward;', Motor, A),
string_concat(A, ';', B),
string_concat(B, Speed, C),
write_canonical(WriteFd, C),
flush_output(WriteFd),
read_line_to_codes(ReadFd, _).
%% nxt_actions_motor_backward(+Motor,+RotSpeed).
%
% Rotates motor backward at specified speed.
nxt_actions_motor_backward(Motor,RotSpeed) :-
Speed is RotSpeed / 9,
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('motor; setReverse;', Motor, A),
string_concat(A, ';', B),
string_concat(B, Speed, C),
write_canonical(WriteFd, C),
flush_output(WriteFd),
read_line_to_codes(ReadFd, _).
%% nxt_actions_motor_get_speed(+Motor,-RotSpeed).
%
% Gets speed of the motor.
nxt_actions_motor_get_speed(Motor,RotSpeed) :-
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('motor; getRotationCount;', Motor, A),
write_canonical(WriteFd, A),
flush_output(WriteFd),
read_line_to_codes(ReadFd, Speed),
RotSpeed is Speed * 9.
%% nxt_motor_stop(+Motor).
%
% Stops motor.
nxt_actions_motor_stop(Motor) :-
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('motor; stop;', Motor, A),
string_concat(A, '; false', B),
write_canonical(WriteFd, B),
flush_output(WriteFd),
read_line_to_codes(ReadFd, _).
%% nxt_actions_motor_rotate(+Motor,+Speed,+Angle).
%
% Rotates motor at specified speed.
% Forward if Angle is positive, backward if Angle is negative.
% Motor will stop, when specified revolution (Angle in degrees) is reached.
nxt_actions_motor_rotate(Motor,RotSpeed,Angle) :-
Speed is RotSpeed / 9,
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('motor; goTo;', Motor, A),
string_concat(A, ';', B),
string_concat(B, Speed, C),
string_concat(C, ';', D),
string_concat(D, Angle, E),
string_concat(E, '; false', F),
write_canonical(WriteFd, F),
flush_output(WriteFd),
read_line_to_codes(ReadFd, _).
%% nxt_actions_touch_sensor(+Port,-Value).
%
% Gets touch sensor reading.
% Returns 1 if pressed, 0 otherwise.
nxt_actions_touch_sensor(Port,Value) :-
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('sensor; setTouch;', Port, A),
write_canonical(WriteFd, A),
flush_output(WriteFd),
read_line_to_codes(ReadFd, _),
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('sensor; getValue;', Port, B),
write_canonical(WriteFd, B),
flush_output(WriteFd),
read_line_to_codes(ReadFd, Value).
%% nxt_actions_sound_sensor(+Port,-Value).
%
% Gets sound sensor reading.
nxt_actions_sound_sensor(Port,Value) :-
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('sensor; setSound;', Port, A),
write_canonical(WriteFd, A),
flush_output(WriteFd),
read_line_to_codes(ReadFd, _),
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('sensor; getValue;', Port, B),
write_canonical(WriteFd, B),
flush_output(WriteFd),
read_line_to_codes(ReadFd, Value).
%% nxt_actions_light_sensor(+Port,-Value).
%
% Gets light sensor reading.
nxt_actions_light_sensor(Port,Value) :-
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('sensor; setLight;', Port, A),
write_canonical(WriteFd, A),
flush_output(WriteFd),
read_line_to_codes(ReadFd, _),
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('sensor; getValue;', Port, B),
write_canonical(WriteFd, B),
flush_output(WriteFd),
read_line_to_codes(ReadFd, Value).
%% nxt_actions_light_sensor_LED(+Port,+Setting).
%
% Sets the LED on if Setting is activate or off if passivate.
nxt_actions_light_sensor_LED(Port,Setting) :-
write('Light sensor LED connected to port '),
write(Port),
write(' has been set to: '),
write(Setting),
write(' state. '),nl.
%% nxt_actions_ultrasonic_sensor(+Port,-Value).
%
% Gets ultrasonic sensor reading.
nxt_actions_ultrasonic_sensor(Port,Value) :-
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('sensor; setSonar;', Port, A),
write_canonical(WriteFd, A),
flush_output(WriteFd),
read_line_to_codes(ReadFd, _),
nxt_actions_sockets(ReadFd, WriteFd),
string_concat('sensor; getSonarValue;', Port, B),
write_canonical(WriteFd, B),
flush_output(WriteFd),
read_line_to_codes(ReadFd, Value).