【发布时间】:2015-06-19 06:28:04
【问题描述】:
这是我的第一个 C 代码。因此,我编写了一个代码,该代码通过串行端口从微控制器(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);
}
【问题讨论】:
-
script?哇....:-) -
您在 PC 上运行 CodeBlocks 中的代码?你有微控制器的模拟器吗?如果没有,我怀疑您是否能够在您的 PC 上进行测试。运行您的代码。你必须在微控制器上运行它。您可能还想了解如何在微控制器上调试代码。
标签: c crash serial-port microcontroller