2015-06-19 36 views
-2

这是我的第一个C代码。因此,我编写了一个代码,通过串口从一个微控制器(pololu maestro)获得来自5个电位计的值,然后从5个电位值移动10个伺服电机。当我在CodeBlocks中编译和运行代码时,没有错误或警告,但是当我运行它时崩溃。它说我的.exe文件已停止运行。如何在微控制器上调试C程序

// 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> 
#include <stdlib.h> 
#include <ctype.h> 
#include <string.h> 
#include <math.h> 

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

// 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[] = {0x90, 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, unsigned short target1, unsigned short target2, unsigned short target3, unsigned short target4, unsigned short target5, unsigned short target6, unsigned short target7, unsigned short target8, unsigned short target9, unsigned short target10) 
{ 
    unsigned char command[] = {170, 12, 31, 10, channel, target1 & 0x7F, target1 >> 7 & 0x7F, target2 & 0x7F, target2 >> 7 & 0x7F, target3 & 0x7F, target3 >> 7 & 0x7F, target4 & 0x7F, target4 >> 7 & 0x7F, target5 & 0x7F, target5 >> 7 & 0x7F, target6 & 0x7F, target6 >> 7 & 0x7F, target7 & 0x7F, target7 >> 7 & 0x7F, target8 & 0x7F, target8 >> 7 & 0x7F, target9 & 0x7F, target9 >> 7 & 0x7F, target10 & 0x7F, target10 >> 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 = "\\\\.\\COM8"; // 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 count1; 
int b_pos; 
int b_l; 
int x[10]; 
int rel; 
int count2; 
int count3; 
int count4; 
int x0[10]; 
int x1; 
int x2; 
int x3; 
int x4; 
int x5; 

for (count1=1; count1<=1000; count1++){ 

    x1 = maestroGetPosition(fd, 0); 
    x2 = maestroGetPosition(fd, 1); 
    x3 = maestroGetPosition(fd, 2); 
    x4 = maestroGetPosition(fd, 3); 
    x5 = maestroGetPosition(fd, 4); 

///////////////////////////////////////////////////////////////////////////////// 

    if(x1 == 0){ 
     b_pos = 0; 
    }else if((x1 <= 102) && (x1 > 0)){ 
     b_pos = 1; 
    }else if((x1 <= 102) && (x1 > 0)){ 
     b_pos = 2; 
    }else if((x1 <= 102) && (x1 > 0)){ 
     b_pos = 3; 
    }else if((x1 <= 102) && (x1 > 0)){ 
     b_pos = 4; 
    }else if((x1 <= 102) && (x1 > 0)){ 
     b_pos = 5; 
    }else if((x1 <= 102) && (x1 > 0)){ 
     b_pos = 6; 
    }else if((x1 <= 102) && (x1 > 0)){ 
     b_pos = 7; 
    }else if((x1 <= 102) && (x1 > 0)){ 
     b_pos = 8; 
    }else if((x1 <= 102) && (x1 > 0)){ 
     b_pos = 9; 
    }else if((x1 <= 102) && (x1 > 0)){ 
     b_pos = 10; 
    } 

///////////////////////////////////////////////////////////////////////////////// 

    if(b_pos != 0){ 
     if (x2 <= 255){ 
      b_l = 1; 
     }else if((x2 <= 510) && (x2 >= 256)){ 
      b_l = 2; 
     }else if((x2 <= 765) && (x2 >= 511)){ 
      b_l = 3; 
     }else{ 
      b_l = 4; 
     } 
    } 

///////////////////////////////////////////////////////////////////////////////// 

    x3 = (x3*0.988)+992; 
    for (count4 = 1; count4 <= 10; count4++){ 
     x[count4] = x3; 
    } 

///////////////////////////////////////////////////////////////////////////////// 

    if(b_pos != 0){ 
     x4 = (x4*0.39)-200; 
     x5 = (x5*0.098); 
     x[b_pos] = x[b_pos] + x4; 
     rel = 1; 

     if (x4 >= 0){ 
      for (count2 = b_pos; count2<=((b_pos) + (b_l)); count2++){ 
         if ((count2 > 0) && (count2 < 11)){ 
          x[count2] = x[count2] + x4 + ((x4/abs(x4))*(x5*rel)); 
          rel += 1; 
         } 
      } 
      for (count2 = b_pos; count2<=((b_pos) - (b_l)); count2--){ 
         if ((count2 > 0) && (count2 < 11)){ 
          x[count2] = x[count2] + x4 + ((x4/abs(x4))*(x5*rel)); 
          rel += 1; 
         } 
      } 
     } 
    } 

///////////////////////////////////////////////////////////////////////////////// 


    for (count3 = 1; count3 <= 10; count3++){ 
     if (x[count3] < 992){ 
      x[count3] = 992; 
     } 
     if (x[count3] > 2000){ 
      x[count3] = 2000; 
     } 
     x0[count3] = (x[count3] * 4); 
    } 


    //int target = (position < 6000) ? 7000 : 5000; 
    //printf("Setting target to %d (%d us).\n", target, target/4); 
    maestroSetTarget(fd, 7, x0[1], x0[2], x0[3], x0[4], x0[5], x0[6], x0[7], x0[8], x0[9], x0[10]); 

} 

return 0; 

close(fd); 

} 
+1

'script'?哇.... :-) –

+0

你在PC上的CodeBlocks中运行代码?你有微控制器的仿真器吗?如果没有,我怀疑你会被打包测试。在PC上运行你的代码。你将不得不在微控制器上运行它。您可能还想了解如何在微控制器上调试代码。 – hoijui

回答

2

你有

x[b_pos] = x[b_pos] + x4; 

其中b_pos可能有10值,但x只能容纳10元素,访问然后11个元素会导致不确定的行为,这反过来又可以让你的程序崩溃。

+0

好的,我将尺寸更改为11,但仍然崩溃。伙计们有什么建议? –

+0

我并不感到惊讶,我建议使用调试器,如果你不知道如何,有大量的在线材料。 –