需要帮助修复pololu maestro的C代码

时间:2015-06-26 09:16:41

标签: c serial-port

我试图使用 C 语言移动多个伺服器。我正在使用pololu mini maestro 24频道enter link description here。我修改了用户手册中提供的代码。我在手册中粘贴了紧凑的协议,用于移动多个伺服系统。但它不起作用。没有警告错误,但代码不起作用。我使用了一个串行监视器并看了一下串行输出,我得到了这个:

  

[b] aa 0c 1f [u] 0d [/ u] 0a 07 70 2e 70 2e 70 2e 70 2e 70 2e 70 2e 70 2e 70 2e 70 2e 70 2e [/ b]

我不知道为什么会有一个' 0d'在那里。它不应该在那里。哦,我从通道0获得了锅位,但我无法移动舵机。如果你可以帮助我,那将是很棒的。

// Uses POSIX functions to send and receive data from a Maestro.
// NOTE: The Maestro's serial mode must be set to "USB Dual Port".
// NOTE: You must change the 'const char * device' line below.

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

#ifdef _WIN32
#define O_NOCTTY 0
#else
#include <termios.h>
#endif
#include <math.h>

int mod (int a, int b)
{
   if(b < 0) //you can check for b == 0 separately and do what you want
     return mod(-a, -b);
   int ret = a % b;
   if(ret < 0)
     ret+=b;
   return ret;
}

// Gets the position of a Maestro channel.
// See the "Serial Servo Commands" section of the user's guide.
int maestroGetPosition(int fd, unsigned char channel)
{
  unsigned char command[] = {170 ,12 ,16 , channel};
  if(write(fd, command, sizeof(command)) == -1)
  {
    perror("error writing");

    return -1;
  }

  unsigned char response[2];
  if(read(fd,response,2) != 2)
  {
    perror("error reading");
    return -1;
  }

  return response[0] + 256*response[1];
}

// Sets the target of a Maestro channel.
// See the "Serial Servo Commands" section of the user's guide.
// The units of 'target' are quarter-microseconds.
int maestroSetTarget(int fd, unsigned char channel, int target[])
{
 unsigned char command[] = {170 ,12 ,31 ,10 ,channel ,     // 0x9f command, 0x0A numbers of channels to move, channel is the first channel number.
            target[1] & 0x7f, target[1] >> 7 & 0x7F,   // channe1 pos, channel 1 speed
            target[2] & 0x7F, target[2] >> 7 & 0x7F,    // ...
            target[3] & 0x7F, target[3] >> 7 & 0x7F,
            target[4] & 0x7F, target[4] >> 7 & 0x7F,
            target[5] & 0x7F, target[5] >> 7 & 0x7F,
            target[6] & 0x7F, target[6] >> 7 & 0x7F,
            target[7] & 0x7F, target[7] >> 7 & 0x7F,
            target[8] & 0x7F, target[8] >> 7 & 0x7F,
            target[9] & 0x7F, target[9] >> 7 & 0x7F,
            target[10] & 0x7F, target[10] >> 7 & 0x7F};// ...

  if (write(fd, command, sizeof(command)) == -1)
  {
    perror("error writing");

    return -1;
  }
  return 0;
}

int main()
{
  // Open the Maestro's virtual COM port.
  const char * device = "\\\\.\\USBSER000";  // Windows, "\\\\.\\COM6" also works
  //const char * device = "/dev/ttyACM0";  // Linux
  //const char * device = "/dev/cu.usbmodem00034567"; // Mac OS X
  int fd = open(device, O_RDWR | O_NOCTTY );
  if (fd == -1){
    perror(device);
    return 1;
  }

#ifndef _WIN32
  struct termios options;
  tcgetattr(fd, &options);
  options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  options.c_oflag &= ~(ONLCR | OCRNL);
  tcsetattr(fd, TCSANOW, &options);
#endif
    int pot = 0;
  int position = maestroGetPosition(fd, pot);
  printf("Current position is %d %d.\n", position, pot);

int x[10];

 x[1] = 6000;
 x[2] = 6000;
 x[3] = 6000;
 x[4] = 6000;
 x[5] = 6000;
 x[6] = 6000;
 x[7] = 6000;
 x[8] = 6000;
 x[9] = 6000;
 x[10] = 6000;

 maestroSetTarget(fd,7, x);

  close(fd);
  return 0;
}

0 个答案:

没有答案