程序来了 调油门中卫用的简单程序(不要用它跑车) //四轮轮毂电机直驱概念车:0版本程序仅用于校准油门行程,及监控转向量 #include //读取两通道接收机信号 //http://arduino.cc/forum/index.php/topic,42286.0.html int ppm1 = 2; //D2 receive ch1 in int ppm2 = 3; //D3 receive ch2 in //接收机两个通道分别接arduino的数字口2、3脚 //中断通道2和3 //Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). //The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18). Servo ST; //D9 steering servo out Servo TH_FL; //D10 ESC out F_Left Servo TH_FR; //D11 ESC out F_Right Servo TH_RL; //D10 ESC out R_Left Servo TH_RR; //D11 ESC out R_Right int val1; //转向输出量 int val_TH; //油门总输出 unsigned long rc1_PulseStartTicks,rc2_PulseStartTicks; volatile int rc1_val, rc2_val; //定义转向量与油门量 void setup() { pinMode(ppm1, INPUT); //监测转向信号 pinMode(ppm2, INPUT); //监测油门信号 //PPM inputs from RC receiver ST.attach(11); //转向舵机通道11号数字接口 TH_FL.attach(9); //油门通道FL TH_FR.attach(6); //油门通道FR TH_RL.attach(10); //油门通道RL TH_RR.attach(5); //油门通道RR attachInterrupt(0, rc1, CHANGE); attachInterrupt(1, rc2, CHANGE); // 电平变化即触发中断 Serial.begin(9600);//启动串口通信,波特率为9600b/s } void rc1() { // did the pin change to high or low? if (digitalRead( ppm1 ) == HIGH) rc1_PulseStartTicks = micros(); // store the current micros() value else rc1_val = micros() - rc1_PulseStartTicks; } void rc2() { // did the pin change to high or low? if (digitalRead( ppm2 ) == HIGH) rc2_PulseStartTicks = micros(); else rc2_val = micros() - rc2_PulseStartTicks; } //中断函数(接收机信号) void loop() { val1 = map(rc1_val, 980, 2000, 59, 141); //转向量 ST.write(val1); //转向输出 val_TH = map(rc2_val, 960, 1992, 30, 150) ; //油门总量(未修改状态) TH_FL.write(val_TH ); TH_FR.write(val_TH ); TH_RL.write(val_TH ); TH_RR.write(val_TH ); //油门输出 //print values Serial.print("channel 1: "); Serial.print(rc1_val); Serial.print(" "); Serial.print("channel 2: "); Serial.println(rc2_val); } //end |
标准模式程序(转相辅助+esp) //四轮轮毂电机直驱概念车:平路程序,转辅助+防滑 #include #include "Wire.h" #include "I2Cdev.h" #include "MPU6050.h" MPU6050 accelgyro; int16_t ax, ay, az; int16_t gx, gy, gz; bool blinkState = false; //陀螺仪程序 //读取两通道接收机信号 //http://arduino.cc/forum/index.php/topic,42286.0.html int ppm1 = 2; //D2 receive ch1 in int ppm2 = 3; //D3 receive ch2 in //接收机两个通道分别接arduino的数字口2、3脚 //中断通道2和3 //Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). //The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18). Servo ST; //D9 steering servo out Servo TH_FL; //D10 ESC out F_Left Servo TH_FR; //D11 ESC out F_Right Servo TH_RL; //D10 ESC out R_Left Servo TH_RR; //D11 ESC out R_Right int val1; //转向输出量 int val_TH; //油门总输出 int val2; //油门输出FL int val3; //油门输出FR int val4; //油门输出RL int val5; //油门输出RR int cc1; //转向辅助 int gyro; //陀螺仪数值 int cc2; //防滑辅助L int cc3; //防滑辅助R //转向辅助用于监测转向舵轮角度,增加外侧后轮转速并减小内侧后轮转速,以帮助车辆入弯,防止推头。 //防滑辅助监测陀螺仪角速度,增加一侧前轮转速,降低另一侧后轮转速,以防止车身打滑时发生自转。 unsigned long rc1_PulseStartTicks,rc2_PulseStartTicks; volatile int rc1_val, rc2_val; //定义转向量与油门量 void setup() { pinMode(ppm1, INPUT); //监测转向信号 pinMode(ppm2, INPUT); //监测油门信号 //PPM inputs from RC receiver ST.attach(11); //转向舵机通道11号数字接口 TH_FL.attach(9); //油门通道FL TH_FR.attach(6); //油门通道FR TH_RL.attach(10); //油门通道RL TH_RR.attach(5); //油门通道RR attachInterrupt(0, rc1, CHANGE); attachInterrupt(1, rc2, CHANGE); // 电平变化即触发中断 Wire.begin(); accelgyro.initialize(); //陀螺仪初始化 Serial.begin(9600);//启动串口通信,波特率为9600b/s } void rc1() { // did the pin change to high or low? if (digitalRead( ppm1 ) == HIGH) rc1_PulseStartTicks = micros(); // store the current micros() value else rc1_val = micros() - rc1_PulseStartTicks; } void rc2() { // did the pin change to high or low? if (digitalRead( ppm2 ) == HIGH) rc2_PulseStartTicks = micros(); else rc2_val = micros() - rc2_PulseStartTicks; } //中断函数(接收机信号) void loop() { accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); //陀螺仪信号 val1 = map(rc1_val, 980, 2000, 59, 141); //转向量 ST.write(val1); //转向输出 val_TH = map(rc2_val, 960, 1992, 30, 150) ; //油门总量(未修改状态) cc1 = map(rc1_val, 980, 2000, -13, 17); // 后轮转速差系数_转向辅助 !!!!! 调整后两位数值,差越大转速差越大。 gyro = gz/131; //陀螺仪数值, -250~+250 cc2 = map(gyro, -250, -1, -42, 0); //向右偏转时,增加FR转速,减低RL转速 cc2 = constrain(cc2, -42, 0); //限制cc2范围 cc3 = map(gyro, -1, 250, 0, 42); //向左偏转时,增加FL转速,减低RR转速 cc3 = constrain(cc3, 0, 42); //限制cc3范围 !!!! 调整倒数第二、和第一位数值,绝对值越大则esp稳定效果越明显。 val2 = val_TH + cc3; //油门FL val3 = val_TH - cc2; //油门FR val4 = val_TH - cc1 + cc2; //油门RL val5 = val_TH + cc1 - cc3; //油门RR TH_FL.write(val2); TH_FR.write(val3); TH_RL.write(val4); TH_RR.write(val5); //油门输出 //print values Serial.print("channel 1: "); Serial.print(rc1_val); Serial.print(" "); Serial.print("channel 2: "); Serial.println(rc2_val); Serial.print(" "); Serial.print("gyro: "); Serial.println(gz/131); Serial.print(" "); Serial.println(cc2); Serial.print(" "); Serial.println(cc3); } //end |
漂移程序(后驱为主,前轮动力适时介入) //四轮轮毂电机直驱概念车:漂移程序,后驱为主,陀螺仪监测角速度加以前轮辅助 //电调刹车设置1,前轴无刹车(FL=9=S3白线,FR=6=S2橘黄),后轴有刹车(RL=10=S4棕线,RR=5=S1红线),初始值90。 //电调刹车设置2,前后有刹车,初始值95。 #include #include "Wire.h" #include "I2Cdev.h" #include "MPU6050.h" MPU6050 accelgyro; int16_t ax, ay, az; int16_t gx, gy, gz; bool blinkState = false; //陀螺仪程序 //读取两通道接收机信号 //http://arduino.cc/forum/index.php/topic,42286.0.html int ppm1 = 2; //D2 receive ch1 in int ppm2 = 3; //D3 receive ch2 in //接收机两个通道分别接arduino的数字口2、3脚 //中断通道2和3 //Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). //The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18). Servo ST; //D9 steering servo out Servo TH_FL; //D10 ESC out F_Left Servo TH_FR; //D11 ESC out F_Right Servo TH_RL; //D10 ESC out R_Left Servo TH_RR; //D11 ESC out R_Right int val1; //转向输出量 int val_TH; //油门总输出 int val2; //油门输出FL int val3; //油门输出FR int val4; //油门输出RL int val5; //油门输出RR int cc1; //转向辅助 int gyro; //陀螺仪数值 int cc2; //防滑辅助 //转向辅助用于监测转向舵轮角度,增加外侧后轮转速并减小内侧后轮转速,以帮助车辆入弯,防止推头。 //防滑辅助监测陀螺仪角速度,增加一侧前轮转速,降低另一侧后轮转速,以防止车身打滑时发生自转。 unsigned long rc1_PulseStartTicks,rc2_PulseStartTicks; volatile int rc1_val, rc2_val; //定义转向量与油门量 void setup() { pinMode(ppm1, INPUT); //监测转向信号 pinMode(ppm2, INPUT); //监测油门信号 //PPM inputs from RC receiver ST.attach(11); //转向舵机通道11号数字接口 TH_FL.attach(9); //油门通道FL TH_FR.attach(6); //油门通道FR TH_RL.attach(10); //油门通道RL TH_RR.attach(5); //油门通道RR attachInterrupt(0, rc1, CHANGE); attachInterrupt(1, rc2, CHANGE); TH_FL.write(90); TH_FR.write(90); delay(1500); Wire.begin(); accelgyro.initialize(); //陀螺仪初始化 Serial.begin(9600);//启动串口通信,波特率为9600b/s } void rc1() { // did the pin change to high or low? if (digitalRead( ppm1 ) == HIGH) rc1_PulseStartTicks = micros(); // store the current micros() value else rc1_val = micros() - rc1_PulseStartTicks; } void rc2() { // did the pin change to high or low? if (digitalRead( ppm2 ) == HIGH) rc2_PulseStartTicks = micros(); else rc2_val = micros() - rc2_PulseStartTicks; } //中断函数(接收机信号) void loop() { accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); //陀螺仪信号 val1 = map(rc1_val, 980, 2000, 59, 141); //转向量 ST.write(val1); //转向输出 val_TH = map(rc2_val, 960, 1992, 30, 150) ; //油门总量(未修改状态) int TH_net =val_TH - 90; //净油门值 TH_net = TH_net * 50 / 100; //后轮油门输出50% !!!!! int TH_Rear = TH_net + 90;//后轮油门值 gyro = gz/131; //陀螺仪数值, -250~+250 cc2 = map(gyro, -100, 100, -60, 60); // !!!!! 监测陀螺仪量,介入前轴动力。陀螺仪可调范围-250~250,cc2值+-60不要改。 cc2 = abs(cc2); int TH_Front = TH_net * cc2; //前轴油门比例,油门量x陀螺介入量(满值60)/60 val2 = 95 + TH_Front/60; //油门FL, val3 = 95 + TH_Front/60; //油门FR, 前轮自由旋转,车身打滑时才介入动力。95为无动力的数值 val4 = TH_Rear; //油门RL val5 = TH_Rear; //油门RR TH_FL.write(val2); TH_FR.write(val3); TH_RL.write(val4); TH_RR.write(val5); //油门输出 //print values Serial.print("channel 1: "); Serial.print(rc1_val); Serial.print(" "); Serial.print("channel 2: "); Serial.println(rc2_val); Serial.print(" "); Serial.print("gyro: "); Serial.println(gz/131); Serial.print(" "); Serial.println(cc2); } //end |
广告投放|联系我们|手机|投稿|Archiver|About us|Advertise|遥控迷模型网|RCFans ( 粤ICP备10210518号-1 )
版权所有 RCFans.com © 2003-2016