Управляем Румбой из-под линукса

Все блоги / Блоги людей 19 апреля 2011 0   

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>

int fd;
char buf[512];

int main(void) {
port_set:
  fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    printf("Error opening port.n");
  } else {
    // Настроим порт
    struct termios options;
    tcgetattr(fd, &options);
    cfsetispeed(&options, B57600);
    cfsetospeed(&options, B57600);
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~PARODD;
    options.c_cflag |= CS8;
    options.c_cflag &= ~HUPCL;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag |= CLOCAL;
    tcsetattr(fd, TCSANOW, &options);

    // Переведем Румбу в режим "полного послушания"   
    buf[0]=0x80;
    write(fd, buf, 1);
    sleep(1);
    buf[0]=0x82;
    write(fd, buf, 1);
   sleep(1);

/*
// Эта последовательность скомандует Румбе ехать вперед и вперед
    buf[0]=0x89;
    buf[1]=0x00;
    buf[2]=0xC8;
    buf[3]=0x80;
    buf[4]=0x00;
    write(fd, buf, 5);
*/

// Эта последовательность команд заставит Румбу бегать по периметру квадрата со стороной примерно в полметра
    for(;;) {
      buf[0]=0x89;
      buf[1]=0x01;
      buf[2]=0x2C;
      buf[3]=0x80;
      buf[4]=0x00;
      buf[5]=0x9C;
      buf[6]=0x01;
      buf[7]=0x90;
      buf[8]=0x89;
      buf[9]=0x01;
      buf[10]=0x2C;
      buf[11]=0x00;
      buf[12]=0x01;
      buf[13]=0x9D;
      buf[14]=0x00;
      buf[15]=0x5A;
      write(fd, buf, 16);
      sleep(1);
    }
  }
}


Откомпилировать программу можно командой
$ gcc roomba.c -o roomba


 Источник:Telegamochka на ЖЖ

Похожие публикации

@
  • bowtiesmilelaughingblushsmileyrelaxedsmirk
    heart_eyeskissing_heartkissing_closed_eyesflushedrelievedsatisfiedgrin
    winkstuck_out_tongue_winking_eyestuck_out_tongue_closed_eyesgrinningkissingstuck_out_tonguesleeping
    worriedfrowninganguishedopen_mouthgrimacingconfusedhushed
    expressionlessunamusedsweat_smilesweatdisappointed_relievedwearypensive
    disappointedconfoundedfearfulcold_sweatperseverecrysob
    joyastonishedscreamtired_faceangryragetriumph
    sleepyyummasksunglassesdizzy_faceimpsmiling_imp
    neutral_faceno_mouthinnocent

Архив публикаций