-
Notifications
You must be signed in to change notification settings - Fork 7
AgPi: Agents on Raspberry Pi
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 of the AgPi project. Through this wrapper, Tartarus agents can access the different peripherals of a Raspberry Pi and hence, transcended its predecessors.
WiringPi-Prolog wrapper source code is maintained here.
- Make sure the latest version of Raspbian OS (or any Debian based OS) is installed on Raspberry Pi. The OS can be downloaded from the following link.
- Install SWI-Prolog on the Raspberry Pi OS (as root user) using the command:
% sudo apt-get install swi-prolog - Download/Copy the WiringPi-Prolog tar file (click 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 confirm, use the following commands on the terminal:
$ swipl?- file_search_path(swi, X). - 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. You can do this by closing all the terminals which are currently running and then starting a new terminal.
- 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.plusing:?- 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.
Below is the pin mapping between AgPi and Raspberry pins. The column wPi is the one which we use and its equivalent pin is under the column BCM. A physical hardware layout is also shown in the below figure.

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
pinModecommand:
Syntax:pinMode(+Pin,+Mode)- Pin: GPIO pin number according to WiringPi.
- Mode: INPUT (0) or OUTPUT (1) mode.
- Use
digitalReadcommand 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
digitalWritecommand 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 AgPi pin mapping) along with a resistance in series.
- Connect negative of led to GPIO pin 0(see AgPi 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.
- Connect the I2C device to the Raspberry Pi.
- Check if the device is connected properly using the command:
$ sudo i2cdetect -y 1
It should show something like:
- The address of the I2C device that we interact with is the one that appears on the table in the above image.
- The commands that can be used to interact with the I2C device are:
-
wiringPiI2CSetup(Addr,Fd): To setup a connection with the device.
- Addr: The address of the device obtained in step 2. Example: 0x68 (refer image in step 2).
- Fd: The value returned by the setup command for furthur referencing to the device. -
wiringPiI2CWriteReg8(Fd,Reg,Val): To write a value in a particular register on the device.
- Fd: The value of Fd obtained from the setup command.
- Reg: Address of the register to which the value is to be written.
- Val: The value to be written to the particular register (reg). -
wiringPiI2CReadReg8(Fd,Reg,Val): To read the output from a particular register on the device.
- Fd: The value of Fd obtained from the setup command.
- Reg: Address of the register from which the value is to be read.
- Val: The value stored in the register returned by the command.
-
Example for IMU:
1. Follow the first three steps of I2C to connect the IMU to Raspberry pi.
2. Now once we get the address we type the following commands:
?- wiringPiI2CSetup(0x68, Fd). --returns the value of Fd (4 in this case) to be used in later commands.
?- wiringPiI2CWriteReg8(4,0x6b,0x00). --sets a value to particular registers (refer to IMU datasheet) to awaken the device.
?- wiringPiI2CWriteReg8(4,0x6c,0x00). --sets a value to particular registers to awaken the device.
?- wiringPiI2CWriteReg8(4,0x74,0x00). --sets a value to particular registers to awaken the device.
?- wiringPiI2CReadReg8(4,0x43,T). --returns the value of stored in 0x43 register.
The commands are mentioned below:
-
delay(t): Used to apply a delay of t milliseconds.
- t: Time in milliseconds.
-
delayMicroseconds(t): Used to apply a delay of t microseconds.
- t: Time in microseconds.
-
millis(T): It returns a number (T) representing the number of milliseconds since the program called one of the AgPi functions. It returns an unsigned 32-bit number which wraps after 49 days.
- T: Time in milliseconds returned by the function.
-
micros(T): It returns a number representing the number of microseconds since the program called one of the AgPi functions. It returns an unsigned 32-bit number which wraps after approximately 71 minutes.
- T: Time in microseconds returned by the function.
Example for LED blinking:
- Commect a LED to Raspberry Pi at GPIO pins 1 (HIGH) and 0 (LOW).
- Write the following code in a prolog file(in the working directory).
Blink_Led - Now start prolog with sudo permission(very important):
$ sudo swipl - Consult the
platform_pi.plusing:?- consult('platform_pi.pl').
This will load all of the Tartarus predicates. - Execute the command:
?- start_peripherals. - Execute the following command to see the LED blink and the output of millis command on terminal.
?- start.