gpt4 book ai didi

c - 如何在单片机上调试C程序

转载 作者:行者123 更新时间:2023-11-30 20:13:24 25 4
gpt4 key购买 nike

这是我的第一个 C 代码。因此,我编写了一段代码,通过串行端口从微 Controller (pololu maestro)获取 5 个电位器的值,然后从 5 个电位器值移动 10 个伺服器。当我编译并运行 CodeBlocks 中的代码时,没有错误或警告,但当我运行时它崩溃了。它说我的 .exe 文件已停止运行。

// 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);

}

最佳答案

你有

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

其中 b_pos 的值为 10,但 x 只能保存 10 元素,访问 then 第 11 个元素会导致未定义的行为,进而可能导致程序崩溃。

关于c - 如何在单片机上调试C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30931344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com