crash - How to debug C program on microcontroller -
this first c code. so, wrote code gets values 5 potentiometers microcontroller(pololu maestro) via serial port , moves 10 servos 5 pot values. when compile , run code in codeblocks, there no errors or warnings, when run crashes. says .exe file has stopped running.
// uses posix functions send , receive data maestro. // note: maestro's serial mode must set "usb dual port". // note: must change '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 position of maestro channel. // see "serial servo commands" section of 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 target of maestro channel. // see "serial servo commands" section of user's guide. // units of 'target' 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 maestro's virtual com port. const char * device = "\\\\.\\com8"; // windows, "\\\\.\\com6" 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; (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; (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){ (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; } } (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; } } } } ///////////////////////////////////////////////////////////////////////////////// (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 %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); }
you have
x[b_pos] = x[b_pos] + x4; where b_pos have value of 10, x can hold 10 elements, accessing 11th element causes undefined behavior in turn make program crash.
Comments
Post a Comment