Would anyone have example code in C for Linux.

Inside the Roomba and Scooba and more, Cool mods, Repair and Upgrades - including the all new iRobot Create Kit. Let's void that warranty baby!

Would anyone have example code in C for Linux.

Postby cjones » May 17th, 2007, 10:43 am

I've had my Red Roomba 4100 for months now, and the only time that I?ve got it to move was when I used a special serial port program that allows me to send numbers out of a serial port by typing them in and clicking a button. I?ve read some tutorials on serial programming, but my programs still can?t get the Roomba to respond. I?m wondering if a bunch of what I?ve read was for Windows and not Linux or another system. Would anyone who programs in C on Linux be willing to show a simple program opening up the serial port and telling the Roomba to move?
Posts: 27
Joined: March 19th, 2007, 8:58 pm
Location: Southern California

Postby zal42 » May 17th, 2007, 2:16 pm

You didn't say what compiler you are using, but Here's a good overview of serial port programming under Linux using gcc. Your main workhorses will be the inb_p() and outb_p() routines to input and output bytes, respectively.

I can grab some sample code if you still need it later this evening when I get home from work. :)
Posts: 43
Joined: April 2nd, 2007, 2:10 pm

Segmentation Fault (core dumped)

Postby cjones » May 18th, 2007, 3:13 pm

I'm compiling with gcc and with -O2 optimisation.
Here's the code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/io.h>
#define PORT1 0x3F8


if (ioperm(PORT1, 4, 1)) {perror("ioperm"); exit(1);}
char a;
outb_p(0 , PORT1 + 1); /* No interupts*/
outb_p(0x80 , PORT1 + 3); /* DLab*/
outb_p(0x02 , PORT1 + 0); /*Baud Rate--Low byte?*/
outb_p(0x03 , PORT1 + 3); /* * 8 Bits, No Parity, 1 Stop Bit */
/*outb_p(0x0B , PORT1 + 4); /* Turn on DTR, RTS, and OUT2 */

/*Roomba Control*/
printf("Roomba go!\n");

outb_p(128, PORT1); /* Begin SCI*/
outb_p(130, PORT1); /* Go To Safe Mode*/
outb_p(132, PORT1); /* Go To Full Mode*/

outb_p(137, PORT1);
outb_p(255, PORT1);
outb_p(56, PORT1);
outb_p(128, PORT1);
outb_p(0, PORT1);

while(a != 'a')
a = getchar();
printf("Roomba stop!\n");

outb_p(137, PORT1);
outb_p(0, PORT1);
outb_p(0, PORT1);
outb_p(0, PORT1);
outb_p(0, PORT1);
I have to use sudo to run it or the ioperm won't work. I get a segmentation fault (core dumped) error, and the Roomba still won't move.
Posts: 27
Joined: March 19th, 2007, 8:58 pm
Location: Southern California

Re: Segmentation Fault (core dumped)

Postby roadknightlabs » May 18th, 2007, 7:16 pm

cjones wrote:I'm compiling with gcc and with -O2 optimisation.

Hi cjones,
You can debug your program by compiling with the debug flag and stepping through it with GDB. I wouldn't use -O2 optimization until the program is working, it will strip out data that would be useful for the debugger.

gcc -o roombacontrol -g roombacontrol.c

Then run gdb with:
gdb roombacontrol

This will load in your executable and the source file so you can go through the program step by step to see where it's faulting.

Then at the gdb command prompt type:
break main
You will see something like this:
(gdb) break main
Breakpoint 1 at 0x2a30: file proc_sonar_sample.c, line 86.
(gdb) run
Starting program: /Users/armadilo/projects/rklabs/roomba/proc_sonar_sample
Reading symbols for shared libraries . done

Breakpoint 1, main (argc=1, argv=0xbffffb30) at proc_sonar_sample.c:86
86 if (argc < 2) {
(gdb) next
87 printf("proc_sonar:Please supply a coordinate list file\n");

(This example is from another program I've been working on but you should get the idea)

So just keep typing "next" until you get to the point where the segfault happens. Your bug is right there.

GDB is a pretty useful tool for seeing where bugs are in a program and you can learn a lot from stepping through your program and seeing how it works.


User avatar
Robot Addict
Posts: 228
Joined: September 7th, 2006, 7:32 pm
Location: San Francisco, CA.

Return to Robotic Hacking

Who is online

Users browsing this forum: bilboso, bugmenot, neato, vic7767 and 153 guests