C Arduino输出引脚输出随机活动
我在一个Arduino项目中工作,该项目控制一辆DIY卡车,该卡车读取RC接收器的输出管脚,并且应该相应地读取两个管脚。引脚连接在采用PWM的电机控制器上 这就是问题所在。我的反向功能很好,但在向前的引脚上,我只得到随机活动。我在用电话 这是代码。问题已发布在下面:C Arduino输出引脚输出随机活动,c,arduino,remote-control,C,Arduino,Remote Control,我在一个Arduino项目中工作,该项目控制一辆DIY卡车,该卡车读取RC接收器的输出管脚,并且应该相应地读取两个管脚。引脚连接在采用PWM的电机控制器上 这就是问题所在。我的反向功能很好,但在向前的引脚上,我只得到随机活动。我在用电话 这是代码。问题已发布在下面: #include <Servo.h> //Create variables for three channels int RXCH[3]; volatile int RXSG[3]; int RXOK[3]; int
#include <Servo.h>
//Create variables for three channels
int RXCH[3];
volatile int RXSG[3];
int RXOK[3];
int PWMSG[3];
byte vooruit;
byte achteruit;
Servo stuur;
int mv = 13;
int ma = 10;
void setup() {
Serial.begin(115200);
stuur.attach(8);
//Assign PPM input pins. The receiver output pins are conected as below to non-PWM Digital connectors:
RXCH[0] = 6; //Throttle
RXCH[1] = 7; //Steering
//RXCH[2] = 5; //Nothing yet
//RXCH[3] = 2; //Nothing yet
//RXCH[4] = 7; //Nothing yet
//RXCH[5] = 8; //Nothing yet
for (int i = 0; i < 3; i++){
pinMode(RXCH[i], INPUT);
}
//TCCR1B = TCCR1B & 0b11111000 | 0x01;
//TCCR2B = TCCR2B & 0b11111000 | 0x01;
}
void loop() {
// Read RX values
for (int i = 0; i < 3; i++){ //For each of the 6 channels:
RXSG[i] = pulseIn(RXCH[i], HIGH, 20000); //Read the receiver signal
if (RXSG[i] == 0) { //Error catching
RXSG[i] = RXOK[i];
} else {
RXOK[i] = RXSG[i];
}
//Substitute the high values to a value between -255 and 255
PWMSG[0] = map(RXSG[0], 1000, 2000, -255, 255);
//Servo values, calibrated according to my steering servo.
PWMSG[1] = map(RXSG[1], 1000, 2000, 24, 169);
//Make sure that the value stays within the desired boundaries.
constrain (PWMSG[i], -255, 255);
//For debugginf purposes
Serial.print(" || Ch: ");
Serial.print(i);
Serial.print(" / PWMSG: ");
Serial.print(PWMSG[i]);
}
delay (5);
// Car goes forwards
if (PWMSG[0] > 40)
{
MV();
}
// Car goes backwards
if (PWMSG[0] < -40)
{
MA();
}
// Car stops
else
{
stopmotor();
}
stuur.write(PWMSG[1]);
Serial.println();
}
void MV()
{
vooruit = PWMSG[0];
analogWrite (mv, vooruit);
digitalWrite (ma, LOW);
Serial.print(" vooruit: ");
Serial.print(vooruit);
}
void MA()
{
achteruit = abs(PWMSG[0]);
analogWrite (ma, achteruit);
digitalWrite (mv, LOW);
Serial.print(" achteruit: ");
Serial.print(achteruit);
}
void stopmotor()
{
digitalWrite (ma, LOW);
digitalWrite (mv, LOW);
}
#包括
//为三个通道创建变量
int-RXCH[3];
volatile int RXSG[3];
int-RXOK[3];
int-PWMSG[3];
字节vooruit;
字节无干扰;
伺服电机;
int mv=13;
int-ma=10;
无效设置(){
序列号开始(115200);
附件(8);
//分配PPM输入引脚。接收器输出引脚连接至非PWM数字连接器,如下所示:
RXCH[0]=6;//节气门
RXCH[1]=7;//转向
//RXCH[2]=5;//还没有
//RXCH[3]=2;//还没有
//RXCH[4]=7;//还没有
//RXCH[5]=8;//还没有
对于(int i=0;i<3;i++){
pinMode(RXCH[i],输入);
}
//TCCR1B=TCCR1B&0b11111000 | 0x01;
//TCCR2B=TCCR2B&0b11111000 | 0x01;
}
void循环(){
//读取接收值
对于(int i=0;i<3;i++){//对于6个通道中的每个通道:
RXSG[i]=pulseIn(RXCH[i],高,20000);//读取接收器信号
如果(RXSG[i]==0){//捕获错误
RXSG[i]=RXOK[i];
}否则{
RXOK[i]=RXSG[i];
}
//将高值替换为介于-255和255之间的值
PWMSG[0]=map(RXSG[0],10002000,-255255);
//伺服值,根据我的转向伺服校准。
PWMSG[1]=map(RXSG[1],10002000,24169);
//确保该值保持在所需边界内。
约束(PWMSG[i],-255,255);
//用于调试目的
连续打印(“| | Ch:”);
连载印刷(一);
串行打印(“/PWMSG:”);
串行打印(PWMSG[i]);
}
延误(5);
//汽车向前行驶
如果(PWMSG[0]>40)
{
MV();
}
//汽车倒车
如果(PWMSG[0]<-40)
{
MA();
}
//汽车站
其他的
{
停止马达();
}
stuur.write(PWMSG[1]);
Serial.println();
}
void MV()
{
vooruit=PWMSG[0];
模拟写入(mv、vooruit);
数字写入(ma,低);
连续打印(“vooruit:”);
串行打印(vooruit);
}
void MA()
{
achteruit=abs(PWMSG[0]);
模拟写入(ma、achteruit);
数字写入(毫伏,低);
连载打印(“achteruit:”);
串行打印(achteruit);
}
空制动马达()
{
数字写入(ma,低);
数字写入(毫伏,低);
}
我真的不知道代码是否被认为是漂亮的,或者我是否在这方面犯了一些基本的错误
这是我的第一个项目之一,我试图以正确的方式进行,发表评论,欢迎所有评论良好的批评
代码应该做什么:
- 向前移动发射器上的斗杆,汽车向前行驶,速度应根据斗杆的位置而定
- 向后移动发射器上的斗杆,汽车向后行驶,速度应取决于斗杆的位置
- 将发射器上的杆向左或向右移动,汽车中的伺服应根据Arduino计算的值作出反应。你可能想知道为什么我不把伺服直接放在发射机上。嗯,那是因为我对这个项目有更多未来的想法,现在我可以更容易地校准它
- 当我向前移动发射器上的控制杆,串行监视器打开时,我在串行监视器上获得正确的值,但针脚13上的LED只是随机闪烁,我必须说非常暗淡
#include <Servo.h>
//Create variables for channels
Servo wheel;
int MFORWARD_PIN = 13;
#define MBACKWARD_PIN 10
#define WHEEL_PIN 8
#define THROTTLE_PIN 6
#define STEERING_PIN 7
void setup() {
Serial.begin(115200);
wheel.attach(WHEEL_PIN);
pinMode(THROTTLE_PIN, INPUT);
pinMode(STEERING_PIN, INPUT);
pinMode(MFORWARD_PIN, OUTPUT);
pinMode(MBACKWARD_PIN, OUTPUT);
//TCCR1B = TCCR1B & 0b11111000 | 0x01;
//TCCR2B = TCCR2B & 0b11111000 | 0x01;
}
void loop() {
int throttle = read_throttle();
int steering = read_steering();
delay (5);
throttle_handle(throttle);
steering_handle(steering);
}
// Read RX values
int read_throttle(){
int throttle = pulseIn(THROTTLE_PIN, HIGH, 20000);
throttle = map(throttle, 1000, 2000, -255, 255); //Substitute the high values to a value between -255 and 255.
constrain (throttle, -255, 255); //Make sure that the value stays within the desired boundaries.
//Serial.println(throttle);
}
int read_steering() {
int steering = pulseIn(STEERING_PIN, HIGH, 20000);
steering = map(steering, 1000, 2000, 24, 169); //Servo values, calibrated according to my steering servo.
constrain (steering, 24, 169); //Make sure that the value stays within the disired boundaries.
//Serial.println("steering");
}
void move_forward(int val) {
analogWrite (MFORWARD_PIN, val);
digitalWrite (MBACKWARD_PIN, LOW);
Serial.print(" vooruit: ");
Serial.print(val);
}
void move_backward(int val)
{
val = abs(val);
analogWrite (MBACKWARD_PIN, val);
digitalWrite (MFORWARD_PIN, LOW);
Serial.print(" achteruit: ");
Serial.print(val);
}
void move_stop()
{
digitalWrite (MFORWARD_PIN, LOW);
digitalWrite (MBACKWARD_PIN, LOW);
}
void throttle_handle(int throttle) {
//Serial.print("throttle");
if (throttle > 40) {
move_forward(throttle);
}
if (throttle < -40) {
move_backward(throttle);
}
else {
move_stop();
}
}
void steering_handle(int steering) {
wheel.write(steering);
// Serial.println("steering:");
// Serial.print(steering);
}
#包括
//为通道创建变量
伺服轮;
int M向前_引脚=13;
#定义MBACKWARD_引脚10
#定义车轮定位销8
#定义节气门单元针脚6
#定义转向U销7
无效设置(){
序列号开始(115200);
车轮。连接(车轮销);
pinMode(油门_引脚,输入);
引脚模式(转向引脚,输入);
引脚模式(M前向_引脚,输出);
引脚模式(MBACKWARD_引脚,输出);
//TCCR1B=TCCR1B&0b11111000 | 0x01;
//TCCR2B=TCCR2B&0b11111000 | 0x01;
}
void循环(){
int throttle=read_throttle();
int转向=读取_转向();
延误(5);
油门手柄(油门);
转向手柄(转向);
}
//读取接收值
int read_throttle(){
int油门=脉冲信号(油门针脚,高,20000);
throttle=map(throttle,10002000,-255,255);//将高值替换为-255和255之间的值。
约束(throttle,-255,255);//确保该值保持在所需的边界内。
//串行打印LN(油门);
}
int read_转向系统(){
内部转向=脉冲信号(转向销,高,20000);
转向=地图(转向,1000200024169);//伺服值,根据我的转向伺服进行校准。
约束(steering,24169);//确保该值保持在指定的边界内。
//序列号println(“转向”);
}
无效向前移动(int val){
模拟写入(MFORWARD_引脚,val);
数字写入(MBACKWARD_引脚,低电平);
连续打印(“vooruit:”);
串行打印(val);
}
无效向后移动(int val)
{
val=abs(val);
模拟写入(MBACKWARD_PIN,val);
数字写入(MFORWARD_引脚,低电平);
连载打印(“achteruit:”);
串行打印(val);
}
无效移动_停止()
{
数字写入(MFORWARD_引脚,低电平);
数字写入(MBACKWARD_引脚,低电平);
}
无效节气门把手(内部节气门){
//串行打印(“节流阀”);
#define NB_INPUT 2
...
for (int i = 0; i<NB_INPUT; ++i) {
...
for (int i=0; i<3; ++i)
RXOK[i] = 0;
PWMSG[i] = map(RXSG[i], 1000, 2000, -255, 255);
#define THROTTLE_IDX 0
#define STEERING_IDX 1
if (i == THROTTLE_IDX)
PWMSG[i] = map(RXSG[i], 1000, 2000, -255, 255);
elif (i == STEERING_IDX)
PWMSG[i] = map(RXSG[i], 1000, 2000, 24, 169);
# add a else statement if you need to do a map for the other values of the array
constrain(PWMSG[i], -255, 255)
#define THROTTLE_PIN 6
#define STEERING_PIN 7
#define WHEEL_PIN 8
#define MFORWARD_PIN 13
#define MBACKWARD_PIN 10
Servo wheel;
// sets up the arduino
void setup() {
Serial.begin(115200);
wheel.attach(WHEEL_PIN);
pinMode(THROTTLE_PIN, INPUT);
pinMode(STEERING_PIN, INPUT);
}
// input data handling
int read_throttle() {
int throttle = pulseIn(THROTTLE_PIN, HIGH, 20000);
return map(throttle, 1000, 2000, -255, 255);
}
int read_steering() {
int steering = pulseIn(STEERING_PIN, HIGH, 20000);
return map(throttle, 1000, 2000, 24, 169);
}
// output actions handling
void move_forward(int val) {
analogWrite(MFORWARD_PIN, val);
digitalWrite(MBACKWARD_PIN, LOW);
// Serial.print...
}
void move_backward(int val) {
analogWrite(MFORWARD_PIN, val);
digitalWrite(MBACKWARD_PIN, LOW);
// Serial.print...
}
void stop_motor() {
digitalWrite(MFORWARD_PIN, LOW);
digitalWrite(MBACKWARD_PIN, LOW);
}
void handle_throttle(int throttle) {
if (throttle > 40)
move_forward(throttle);
elif (throttle < -40)
move_backward(throttle);
else
stop_motor();
}
// general algorithm
void loop() {
int throttle = read_throttle();
delay(5);
handle_throttle(throttle);
}
void handle_steering(int steering) {
if (steering > NN)
turn_left(steering);
elif (steering < NN)
turn_right(steering);
else
keep_straight();
}
void loop() {
int throttle = read_throttle();
int steering = read_steering();
delay(5);
handle_throttle(throttle);
handle_steering(steering);
}
int read_input(int i) {
int value = pulseIn(PIN[i], HIGH, 20000);
return map(value, 1000, 2000, DOM_MIN[i], DOM_MAX[i]);
}
int handle_action(int i, int value) {
if (value > BOUND_MIN[i])
*(ACTION[i])(value);
elif (value < BOUND_MAX[i])
*(ACTION[i])(value);
else
*(ACTION[i])(-1);
}
void loop() {
for (int i=0; i<NB_INPUTS; ++i) {
int value = read_input(i);
delay(5);
handle_action(i, value);
}
}