-
Notifications
You must be signed in to change notification settings - Fork 7
AgPi: Agents on Raspberry Pi
Tushar Semwal edited this page Oct 14, 2017
·
21 revisions
WiringPi is a C library which provides APIs to access the GPIO pins and other peripherals of Raspberry Pi. Thanks to the efforts of Midhul Verma and Nikhil Teja Alamanda, a WiringPi-Prolog wrapper was created as a part AgPi project. Through this wrapper, Tartarus agents can access the different peripherals of a Raspberry Pi. WiringPi-Prolog wrapper is hosted here.
- Install the latest version of Raspbian on Raspberry Pi. The OS can be downloaded from the following link .
- Install swi-prolog on the system (as root user) using the command:
% sudo apt-get install swi-prolog
- Download/Copy the WiringPi-Prolog tar file (provided here) onto the Raspberry pi.
- Unzip the WiringPi-Prolog zip file using the command:
$ unzip <file_name>
- Check the swi-prolog installation directory. Generally the location is ‘/usr/lib/swi-prolog’. To check otherwise use the following commands:
$ swipl
?- file_search_path(swi, X). //returns installation directory.
- Copy the wipi folder from the unzipped folder(step 4) to the folder returned in step 5.
- Get out of superuser if still in it.
- Go to your working directory( the folder where you will be working with your project files).
- Copy the ‘platform_pi.pl’ (provided here) file in this directory.
- Now start prolog with sudo permission(very important):
$ sudo swipl
- Consult the
platform_pi.pl
using:?- consult('platform_pi.pl').
This will load all of the Tartarus predicates. - Execute the command:
?- start_peripherals.
This will start the Peripheral Interface. Make sure no errors occur during this step. - Now the peripheral interface is ready, and you can use WiringPi Prolog predicates to control the GPIO pins.
Since AgPi is a wrapper over WiringPi, the commands used in WiringPi are same as in AgPi but just comes with a flavor of Prolog.
The arguments passed to a predicate can take three forms - Input, Output or Both, and are denoted by symbols +, - and ? respectively.
- Set the mode of a pin to input or output using the
pinMode
command:
Syntax:pinMode(+Pin,+Mode)
- Pin: GPIO pin number according to WiringPi.
- Mode: INPUT (0) or OUTPUT (1) mode.
- Use
digitalRead
command to read the input of a particular pin.
Syntax:digitalRead(+Pin,-ValueReturned)
- Pin: GPIO pin number according to WiringPi.
- ValueReturned: Returns a value - either HIGH (1) or LOW (0) for the pin.
- Use
digitalWrite
command to write a value to the particular pin.
Syntax:digitalWrite(+Pin,+Value)
- Pin: GPIO pin number according to WiringPi.
- ValueReturned: set value HIGH (1) or LOW (0) for the pin.
Example for LED:
- Connect positive of led to GPIO pin 1(see WiringPi pin mapping) along with a resistance in series.
- Connect negative of led to GPIO pin 0(see WiringPi pin mapping).
- Write the following code to light up the LED:
?- pinMode(0,0).
--set pinmode of pin 0 to 0 for output.?- pinMode(1,0).
--set pinmode of pin 1 to 0 for output.?- digitalWrite(0,0).
--set value of pin 0 to 0 for LOW/ground.?- digitalWrite(1,1).
--set value of pin 1 to 1 for HIGH/(3.3v). - This will light up the LED.
- To turn it off use the command
digitalWrite(1,0)
to write value 0 to pin 1.