标签:.net net 程序 driver react add href set ice
最近打算折腾飞控,找了找资料和淘宝,发现一个飞控遥控器都要500起步,实在莫名奇妙,研究了下,没啥新奇,就自己做吧
看看效果,只是原型,还要做PCB,目前尺寸还是很占空间的。
遥控器:1个arduino UNO+2个旋转电位器开关+1个2.4G信号发射器+1块面包板
接收端:1个LED灯+1个舵机+1个arduino UNO+1个2.4G信号接收器+1块面包板
元器件都是能买到的,单算遥控器端的话,成本在30-40之间。
#include <SPI.h> #include <Mirf.h> #include <nRF24L01.h> #include <MirfHardwareSpiDriver.h> void setup() { // put your setup code here, to run once: Serial.begin(9600); Mirf.cePin = 9; //设置CE引脚为D9 Mirf.csnPin = 10; //设置CE引脚为D10 Mirf.spi = &MirfHardwareSpi; //加载24L01 SPI Mirf.init(); //初始化nRF24L01 Mirf.setRADDR((byte *)"contr"); Mirf.payload = 2 * sizeof(int); //发送通道,可以填0~128,收发必须一致。 Mirf.channel = 3; Mirf.config(); //这里标识写入了Sender.ino Serial.println("I‘m controller..."); } void loop() { int targetValue1=analogRead(A0); int targetValue2=analogRead(A1); int adaptedValue1=map(targetValue1, 0, 1024, 0, 500); int adaptedValue2=map(targetValue2, 0, 1024, 0, 181); send_data((byte *)"react", adaptedValue1, adaptedValue2); delay(20); } void send_data(byte * targetName, int dt1, int dt2) { byte data[Mirf.payload]; data[0] = dt1; data[1] = dt2; Mirf.setTADDR(targetName); Mirf.send(data); //Mirf.send((byte *)&dt); //while死循环等待发送完毕,才能进行下一步操作。 //Serial.println("-->"); while (Mirf.isSending()) { //Serial.print("."); delay(1); } Serial.print(data[0]); Serial.print(" "); Serial.print(data[1]); Serial.println(""); //Serial.println("OK"); }
#include <SPI.h> #include <Mirf.h> #include <nRF24L01.h> #include <MirfHardwareSpiDriver.h> #include <Servo.h> Servo myservo; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(5, OUTPUT); myservo.attach(9); Mirf.cePin = 4; //设置CE引脚为D4 Mirf.csnPin = 10; //设置CE引脚为D10 Mirf.spi = &MirfHardwareSpi; //加载24L01 SPI Mirf.init(); //初始化nRF24L01 Mirf.setRADDR((byte *)"react"); Mirf.payload = 2 * sizeof(int); //发送通道,可以填0~128,收发必须一致。 Mirf.channel = 3; Mirf.config(); //这里标识写入了Sender.ino Serial.println("I‘m reactor..."); } int dt1=0; int dt2=0; void loop() { // put your main code here, to run repeatedly: byte data[Mirf.payload]; if (Mirf.dataReady()) //等待接收数据准备好 { Mirf.getData(data); //接收数据到data数组 dt1 = (int)data[0]; dt2 = (int)data[1]; Serial.print(dt1); Serial.print(" "); Serial.print(dt2); Serial.println(""); } output_pwm(5, dt1); myservo.write(dt2); } void output_pwm(int pin, int pwm) { int highTime=map(pwm, 0, 500, 0, 1000); int lowTime=1000-highTime; digitalWrite(pin, HIGH); delayMicroseconds(highTime); digitalWrite(pin, LOW); delayMicroseconds(lowTime); }
综上所述,其实就是把2个模拟值(分别来源于2个旋转电位器)放入byte[] 数组中,用2.4G发送出去(类似PPM)。
用到的库函数文件下载地址:Arduino驱动程序-库函数.rar
好了,后续再迭代。
标签:.net net 程序 driver react add href set ice
原文地址:https://www.cnblogs.com/aarond/p/flycontroller.html