這是我的第一個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);
}
'script'?哇.... :-) –
你在PC上的CodeBlocks中運行代碼?你有微控制器的仿真器嗎?如果沒有,我懷疑你會被打包測試。在PC上運行你的代碼。你將不得不在微控制器上運行它。您可能還想了解如何在微控制器上調試代碼。 – hoijui