2015-06-19 2 views
-2

это мой первый код C. Итак, я написал код, который получает значения от 5 потенциометров от микроконтроллера (pololu maestro) через последовательный порт, а затем перемещает 10 сервомашин из 5 значений банка. Когда я компилирую и запускаю код в 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'? Wow .... :-) –

+0

вы запускаете код в CodeBlocks на своем ПК? у вас есть эмулятор для микроконтроллера? если нет, я сомневаюсь, что вы будете кипы для тестирования. Запустите свой код на своем ПК. вам придется запускать его на микроконтроллере. вы также можете прочитать о том, как отлаживать свой код на микроконтроллере. – hoijui

ответ

2

Вы

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

где b_pos может иметь значение 10, но x может содержать только 10 элементы, доступ затем 11 й элемент вызывает неопределенное поведение, которое в свою очередь, может сделать ваш сбой программы.

+0

Итак, я изменил размер до 11, и он по-прежнему падает. Ребята получили какие-то предложения? –

+0

Я не удивлен, я предлагаю использовать отладчик, если вы не знаете, как много материалов в Интернете. –

Смежные вопросы