% % Implementation by Piotr Hołownia % % 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_icommand,[ 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_actions_voltage/1, nxt_actions_voltage_millivolt/1, nxt_actions_start_program/1, nxt_actions_stop_program/0, nxt_actions_play_sound_file/2, nxt_actions_stop_sound_playback/0, nxt_actions_play_tone/2, nxt_actions_get_brick_name/1, nxt_actions_set_brick_name/1 ]). /** NXT Mindstroms - Actions iCommand. Motors: 'A', 'B', 'C'. Sensors ports: 'S1', 'S2', 'S3', 'S4'. @author Piotr Hołownia @license GNU General Public License */ :- dynamic nxt_actions_touch_sensor_connected/2, nxt_actions_sound_sensor_connected/2, nxt_actions_light_sensor_connected/2, nxt_actions_ultrasonic_sensor_connected/2. %% nxt_actions_connection_open % % Opens connection via Bluetooth using iCommand. nxt_actions_connection_open :- jpl_call('icommand.nxt.comm.NXTCommand','open',[],_), writeln('Connected via Bluetooth using iCommand!'). %% nxt_actions_connection_close % % Closes connection via Bluetooth using iCommand. nxt_actions_connection_close :- jpl_call('icommand.nxt.comm.NXTCommand','close',[],_), writeln('Disconnected!'). %% nxt_actions_motor_forward(+Motor,+Speed). % % Rotates motor forward at specified speed. nxt_actions_motor_forward(Motor,Speed) :- jpl_get('icommand.nxt.Motor',Motor,MotorHandle), jpl_call(MotorHandle,'setSpeed',[Speed],_), jpl_call(MotorHandle,'forward',[],_). %% nxt_actions_motor_backward(+Motor,+Speed). % % Rotates motor backward at specified speed. nxt_actions_motor_backward(Motor,Speed) :- jpl_get('icommand.nxt.Motor',Motor,MotorHandle), jpl_call(MotorHandle,'setSpeed',[Speed],_), jpl_call(MotorHandle,'backward',[],_). %% nxt_actions_motor_get_speed(+Motor,+Speed). % % Gets speed of the motor. nxt_actions_motor_get_speed(Motor,Speed) :- jpl_get('icommand.nxt.Motor', Motor, MotorHandle), jpl_call(MotorHandle, 'getSpeed', [], Speed). %% nxt_actions_motor_stop(+Motor). % % Stops motor. nxt_actions_motor_stop(Motor) :- jpl_get('icommand.nxt.Motor',Motor,MotorHandle), jpl_call(MotorHandle,'stop',[],_), jpl_call(MotorHandle,'setSpeed',[0],_). %% 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,Speed,Angle) :- jpl_get('icommand.nxt.Motor',Motor,MotorHandle), jpl_call(MotorHandle,'setSpeed',[Speed],_), jpl_call(MotorHandle,'rotate',[Angle,@(true)],_), jpl_call(MotorHandle,'setSpeed',[0],_). %% nxt_actions_touch_sensor(+Port,-Value). % % Gets touch sensor reading. % Returns 1 if pressed, 0 otherwise. nxt_actions_touch_sensor(Port,Value) :- not(nxt_actions_touch_sensor_connected(Port,_)), retractall(nxt_actions_touch_sensor_connected(_,_)), jpl_get('icommand.nxt.SensorPort',Port,PortHandle), jpl_new('icommand.nxt.TouchSensor',[PortHandle],SensorHandle), assert(nxt_actions_touch_sensor_connected(Port,SensorHandle)), nxt_actions_touch_sensor(_,Value). nxt_actions_touch_sensor(_,Value) :- nxt_actions_touch_sensor_connected(_,SensorHandle), jpl_call(SensorHandle,'isPressed',[],Pressed), nxt_actions_touch_sensor_return(Pressed,Value). nxt_actions_touch_sensor_return(Pressed,Value) :- Pressed == @(false), Value is 0. nxt_actions_touch_sensor_return(Pressed,Value) :- Pressed == @(true), Value is 1. %% nxt_actions_sound_sensor(+Port,-Value). % % Gets sound sensor reading. nxt_actions_sound_sensor(Port,Value) :- not(nxt_actions_sound_sensor_connected(Port,_)), retractall(nxt_actions_sound_sensor_connected(_,_)), jpl_get('icommand.nxt.SensorPort',Port,PortHandle), jpl_new('icommand.nxt.SoundSensor',[PortHandle],SensorHandle), assert(nxt_actions_sound_sensor_connected(Port,SensorHandle)), nxt_actions_sound_sensor(_,Value). nxt_actions_sound_sensor(_,Value) :- nxt_actions_sound_sensor_connected(_,SensorHandle), jpl_call(SensorHandle,'getdBA',[],DBA), Value is DBA. %% nxt_actions_light_sensor(+Port,-Value). % % Gets light sensor reading. nxt_actions_light_sensor(Port,Value) :- not(nxt_actions_light_sensor_connected(Port,_)), retractall(nxt_actions_light_sensor_connected(_,_)), jpl_get('icommand.nxt.SensorPort',Port,PortHandle), jpl_new('icommand.nxt.LightSensor',[PortHandle],SensorHandle), assert(nxt_actions_light_sensor_connected(Port,SensorHandle)), nxt_actions_light_sensor(_,Value). nxt_actions_light_sensor(_,Value) :- nxt_actions_light_sensor_connected(_,SensorHandle), jpl_call(SensorHandle,'getLightPercent',[],Percent), Value is Percent. %% 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) :- not(nxt_actions_light_sensor_connected(Port,_)), retractall(nxt_actions_light_sensor_connected(_,_)), jpl_get('icommand.nxt.SensorPort',Port,PortHandle), jpl_new('icommand.nxt.LightSensor',[PortHandle],SensorHandle), assert(nxt_actions_light_sensor_connected(Port,SensorHandle)), nxt_actions_light_sensor_LED(_,Setting). nxt_actions_light_sensor_LED(_,activate) :- nxt_actions_light_sensor_connected(_,SensorHandle), jpl_call(SensorHandle,'activate',[],_). nxt_actions_light_sensor_LED(_,passivate) :- nxt_actions_light_sensor_connected(_,SensorHandle), jpl_call(SensorHandle,'passivate',[],_). %% nxt_actions_ultrasonic_sensor(+Port,-Value). % % Gets ultrasonic sensor reading. nxt_actions_ultrasonic_sensor(Port,Value) :- not(nxt_actions_ultrasonic_sensor_connected(Port,_)), retractall(nxt_actions_ultrasonic_sensor_connected(_,_)), jpl_get('icommand.nxt.SensorPort',Port,PortHandle), jpl_new('icommand.nxt.UltrasonicSensor',[PortHandle],SensorHandle), assert(nxt_actions_ultrasonic_sensor_connected(Port,SensorHandle)), nxt_actions_ultrasonic_sensor(_,Value). nxt_actions_ultrasonic_sensor(_,Value) :- nxt_actions_ultrasonic_sensor_connected(_,SensorHandle), jpl_call(SensorHandle,'getDistance',[],Distance), Value is Distance. %% nxt_actions_voltage(-Voltage). % % Returns battery voltage in V. nxt_actions_voltage(Voltage) :- jpl_call('icommand.nxt.Battery','getVoltage',[],Voltage). %% nxt_actions_voltage_millivolt(-Voltage). % % Returns battery voltage in mV. nxt_actions_voltage_millivolt(Voltage) :- jpl_call('icommand.nxt.Battery','getVoltageMilliVolt',[],Voltage). %% nxt_actions_start_program(+File) % % Starts a Lego executable file on the NXT. % DOES NOT WORK PROPERLY!! nxt_actions_start_program(File) :- jpl_call('icommand.nxt.FileSystem','startProgram',[File],_). %% nxt_actions_stop_program. % % Stops the currently running Lego executable on the NXT. % DOES NOT WORK PROPERLY!! nxt_actions_stop_program :- jpl_call('icommand.nxt.FileSystem','stopProgram',[],_). %% nxt_actions_play_sound_file(+File,+Repeat). % % Plays a sound file from the NXT. % Files use the .rso extension. % The filename is not case sensitive. % Plays sound once if Repeat is true % or till nxt_actions_stop_sound_playback is used if Repeat is % false. % DOES NOT WORK PROPERLY!! nxt_actions_play_sound_file(File,true) :- jpl_call('icommand.nxt.Sound','playSoundFile',[File,@(true)],_). nxt_actions_play_sound_file(File,false) :- jpl_call('icommand.nxt.Sound','playSoundFile',[File,@(false)],_). %% nxt_actions_stop_sound_playback. % % Stops a sound file that has been playing/repeating. % DOES NOT WORK PROPERLY!! nxt_actions_stop_sound_playback :- jpl_call('icommand.nxt.Sound','stopSoundPlayback',[],_). %% nxt_actions_play_tone(Frequency,Duration). % % Plays tone at specified frequency. % DOES NOT WORK PROPERLY!! nxt_actions_play_tone(Frequency,Duration) :- jpl_call('icommand.nxt.Sound','playTone',[Frequency,Duration],_). %% nxt_actions_get_brick_name(-Name). % % Gets the name of the brick. % DOES NOT WORK PROPERLY!! nxt_actions_get_brick_name(Name) :- jpl_call('icommand.nxt.NXT','getBrickName',[],Name). %% nxt_actions_set_brick_name(+Name) % % Sets the name of the brick. % DOES NOT WORK PROPERLY!! nxt_actions_set_brick_name(Name) :- jpl_call('icommand.nxt.NXT','setBrickName',[Name],_).