Martin Meo LAB #1 – The Handy Cricket 91.548 ROBOT DESIGN 02-23-04
EXERCISE 1
Experiment with the Handy Cricket and the Cricket Logo software. Play with motor and sensor commands. Display sensor data back on the Cricket Logo screen.
I wrote a program using cricket logo that uses one motor and one CDS light sensor. The program turns on the motor when the sensor detects bright light and turns off the motor when the sensor detects less light.
The same program also sends the value of the light sensor to the IR communication port. The sensor value ranges from 0 to 255, where 0 is maximum brightness and 255 is minimum. The value is then sent to the serial cricket which sends it to COM1 on the PC. The PC is running the
Logo Monitor and displays the values. See Program Listing #1.1
EXERCISE 2, 3
Experiment with the Cricket’s IR communication primitives send , newir?
, and ir .
Get one Cricket to trigger another Cricket to do something.
I wrote a program to send integer values from one Cricket to another. The Cricket #1 generates random values from 0 to 3 and sends them via the IR port. Cricket #2 listens to its IR port and beeps N times when it receives a value.
I wrote the program is such a way that I could download the exact same code to both Cricket #1 and Cricket #2. At run-time each Cricket checks its sensor A value to determine it’s identity. If a Cricket does not have sensor A connected it will return a value of 255 and will designate itself as Cricket #2. If sensor A does not equal 255 that Cricket is designated Cricket #1. See Program
Listing #1.3
EXERCISE 5
Put a program on the Cricket that continually transmits sensor values. Write a program that runs on a PC that reads values from the serial port and displays them is some visual fashion.
I wrote a program using the Cygwin Linux emulator and the gcc compiler to read from the COM port and display a text bar graph on the console screen that represents the sensor value, followed by the value itself. The sensor value was scaled to fit the number of columns available and to make brighter light give a larger display. Getting the serial communication stuff to run on the
PC took a lot of time because of the new development environment. See Program Listing #1.4 and Sample Output.
This was the first lab of the first graduate level class I have taken. I came to the class two weeks late and unfamiliar with most of the specific tools used at the lab. I want to acknowledge the other students in the class and Fred for their help, support and encouragement and several “crash courses” in the bewildering world of Cygwin, Emacs and gcc.
Martin Meo LAB #1 – The Handy Cricket 91.548 ROBOT DESIGN 02-23-04
PROGRAM LISTING #1.1
// Martin Meo
// Lab 1.1 UML 91.548
// Program uses one motor and one sensor.
// Turns motor ON when the light is on, and turns motor OFF
// when light is OFF.
// Displays light on condition (0=off) on the
Criket Monitor to main loop
[
send LightOn
ifelse LightOn
[a, on]
[a, off]
] end to LightOn
output sensora < 50 end
PROGRAM LISTING #1.2
// Martin Meo
// Lab 1.2 UML 91.548
// Program uses one motor and one sensor.
// Sets up a backgroud task that beeps when the light goes from off to on
// meanwhile it cycles the motor back and forth. to main when [LightOn] [beep] loop [a, onfor 5 rd] end to LightOn
output sensora < 50 end
Martin Meo LAB #1 – The Handy Cricket 91.548 ROBOT DESIGN 02-23-04
PROGRAM LISTING #1.3
// Martin Meo
// Lab 1.3 UML 91.548
// Program uses IR transceiver to transmit and receive numbers
// between two crickets. The same program is downloaded to two
// crickets.
// Cricket 1 must have sensora installled such that sensora value <> 255
// Cricket 2 must have no sensora installed, such that sensora value = 255
// Cricket 1 sends random numbers once per second.
// Cricket 2 listens to IR and beeps N times when it hears a number.
// PC serial port with serial cricket can be positioned to "eavsdrop"
// and display number on Criket Monitor to confirm numbers. global [cnt] to main ifelse sensora = 255 [cricket2] [cricket1] end to cricket1
beep
loop
[
setcnt random % 4
send cnt
wait 10
] end to cricket2
beep
loop
[
beeps getbyte
] end to GetByte
waituntil [newir?]
output ir end to Beeps :n
repeat :n [note 110 1 wait 1] end
Martin Meo LAB #1 – The Handy Cricket 91.548 ROBOT DESIGN 02-23-04
PROGRAM LISTING #1.4
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <tcl.h>
/*
Martin Meo UML 91.548 Roboticxs LAB 1 February
21, 2004
This program receives data from the serial port and displays the values as a bar graph.
Sensor data is sent by a Handy Cricket via its IR port to COM1 of the PC.
*/ int OpenSerialPort(void); int WriteByteSerialPort(int, unsigned char); int ReadByteSerialPort (int, unsigned char*); int DisplayBarGraph (int value); int main (void)
{
int i, hPort, bytes, err;
unsigned char dataBuffer[256], databyte;
printf("Hello World!\n");
hPort= OpenSerialPort();
// tcflush(hPort, TCIOFLUSH);
// WriteByteSerialPort(hPort, 5);
while(1)
{
// usleep(400000);
err= ReadByteSerialPort(hPort, &databyte);
if (err==0)
{
DisplayBarGraph(64-databyte/4);
printf("%d\n",databyte);
}
}
} int OpenSerialPort(void)
{ int hPort; hPort = open("/dev/ttyS0", // COM1=
"/dev/ttyS0" COM2= "/dev/ttyS1"
O_RDWR | O_NOCTTY | O_NDELAY);
// Cannot open serial port
{ if (hPort == -1) perror("Error opening serial port");
} else port opened successfully
// Serial
{
fcntl(hPort, F_SETFL, 0); //
Causes read()to wait for bytes at port
// fcntl(hPort, F_SETFL, FNDELAY);
// Causes read()to return immediately
perror("Serial port opened successfully.");
} return (hPort);
} int WriteByteSerialPort(int hPort, unsigned char data)
{
write(hPort, &data, 1);
tcflush(hPort, TCIOFLUSH);
return (0);
Martin Meo LAB #1 – The Handy Cricket 91.548 ROBOT DESIGN 02-23-04
} int ReadByteSerialPort (int hPort, unsigned char* byte)
{
/*
Reads one character from the serial port, nonblocking.
RETURN VALUE
(0) BYTE READ, OK
(-1) NO DATA AT PORT, OK
(-2) I/O ERROR
*/
int err;
fcntl(hPort, F_SETFL, FNDELAY); // Causes read()to return immediately
err= read(hPort, byte, 1); if (err==-1)
{
if (errno == EAGAIN)
{return(-1);} // No bytes at port, okay (-1)
else {return(-2);} // I/O error
(-2)
}
return(0); // Byte read, okay
(0)
} int CloseSerialPort (int hPort)
{ close(hPort);
} int DisplayBarGraph(int value)
{
int i;
for (i=0; i<value; i++)
{
printf("%c", 'x');
}
}
Sample Output #1.4 csrobot@UMASS-BC65F3A5D /home/martin/work/Serial
$ ./SD
Hello World!
Serial port opened successfully.: No error xx250 xx250 xxx246 xxxxxx234 xxxxxxxxxxxxxxxxxx185 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx103 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx71 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx76 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx97 xxxx243 xxx247 xxx246 xxx246 xxxx240 xxxxxxxx226 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx75 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx72 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx67 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx68 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx77 xxxxxxxxx220 xxxxx239 xxxxx238 xxxxx236