Monday, 3 January 2011

A nasty piece of work robot!

In which I'm trying to write a Lisp program  to control an industrial robot...
I warn you to read on: I'm a perfect noob in the complex lisp world or better Lisp (to say Common Lisp), the informations that I'll dare to write on this blog have to be carefully weighted. I'm waiting for you to write down your comments and hints on my humble (and I hope useful) work. Thank you!!!

In the c language (and lesser in the c++) the interaction with OS could occur at very low level. In systems like Linux one could take advantages of this to have the full control of the hardware. In the following example I'll show you a simple robot connected via serial port to a stand-alone PC system. PC sends commands to robot that executes them and sends back some informations to PC. The communication protocol is based on binary packets assembled like this:
  1. copy the old device configuration;
  2. modify the current device configuration;
  3. write the new device configuration;
  4. required device I/O;
  5. write old device configuration;
  6. close device.
    today I'd like to introduce my small functions to achieve 1, 2, 3, 4, 6 and 7:

    (defun open-serial-port (tty-name)
      (if (stringp tty-name)
          (sb-posix:open tty-name
                 (boole boole-ior (boole boole-ior sb-posix:O-RDWR sb-posix:O-NOCTTY) sb-posix:O-NONBLOCK) sb-posix:O-NDELAY)
          (error "Wrong device name.")))

    (defun close-serial-port (fd)
      (if (>= fd 0)
        (sb-posix:fcntl fd sb-posix:F-SETFL 0)
        (sb-posix:close fd))
          (error "Wrong file descriptor value.")))

    (defun get-serial-port-config (fd)
      (if (>= fd 0)
          (sb-posix:tcgetattr fd)
          (error "Wrong file descriptor value.")))

    (defun set-serial-port-config (config &optional iflag oflag cflag lflag)
      (if (not (eql config nil))
          ((not (eql iflag nil)) (setf (sb-posix:termios-iflag config) iflag))
          ((not (eql oflag nil)) (setf (sb-posix:termios-oflag config) oflag))
          ((not (eql cflag nil)) (setf (sb-posix:termios-cflag config) cflag))
          ((not (eql lflag nil)) (setf (sb-posix:termios-lflag config) lflag)))
          (error "Wrong configuration.")))

    I'd like to thanks:
    Ryan's Tech Blog
    SBCL Site

      No comments:

      Post a Comment