Qian Yuyang
/
Fly_test
NRF+MPU6050
main.cpp@3:46535ec6d8b1, 2019-03-18 (annotated)
- Committer:
- AlexQian
- Date:
- Mon Mar 18 03:39:29 2019 +0000
- Revision:
- 3:46535ec6d8b1
- Parent:
- 2:4704fdd9ef91
- Child:
- 4:1b985e622d26
fly-blocky-template
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AlexQian | 3:46535ec6d8b1 | 1 | #define HIGH 1 |
AlexQian | 3:46535ec6d8b1 | 2 | #define LOW 0 |
Owen | 0:a51a6e7da590 | 3 | #include "mbed.h" |
Owen | 0:a51a6e7da590 | 4 | #include "nRF24L01P.h" |
AlexQian | 3:46535ec6d8b1 | 5 | #include "MPU6050.h" |
AlexQian | 3:46535ec6d8b1 | 6 | #include <string> |
AlexQian | 3:46535ec6d8b1 | 7 | typedef bool boolean; |
AlexQian | 3:46535ec6d8b1 | 8 | typedef std::string String; |
Owen | 0:a51a6e7da590 | 9 | |
AlexQian | 3:46535ec6d8b1 | 10 | char rxdata[12] ; |
AlexQian | 3:46535ec6d8b1 | 11 | float Yaw,Pitch,Roll,Power; |
AlexQian | 3:46535ec6d8b1 | 12 | int Yaw_t; |
AlexQian | 3:46535ec6d8b1 | 13 | int Pitch_t; |
AlexQian | 3:46535ec6d8b1 | 14 | int Roll_t; |
AlexQian | 3:46535ec6d8b1 | 15 | int Power_t; |
AlexQian | 3:46535ec6d8b1 | 16 | long CycleCount; |
AlexQian | 3:46535ec6d8b1 | 17 | long ReceiveCount; |
Owen | 0:a51a6e7da590 | 18 | |
AlexQian | 3:46535ec6d8b1 | 19 | #define NRF24_TRANSFER_SIZE 12 |
AlexQian | 3:46535ec6d8b1 | 20 | nRF24L01P nrf24_PB_15(PB_15,PB_14,PB_13,PB_6,PB_5,PB_7); |
AlexQian | 3:46535ec6d8b1 | 21 | Serial Serial_1(PA_2,PA_3); |
AlexQian | 3:46535ec6d8b1 | 22 | DigitalOut myDigitalOutPA_7(PA_7); |
AlexQian | 3:46535ec6d8b1 | 23 | DigitalOut myDigitalOutPA_6(PA_6); |
AlexQian | 3:46535ec6d8b1 | 24 | MPU6050 mpu6050(PB_9,PB_8); |
Owen | 0:a51a6e7da590 | 25 | int main() { |
AlexQian | 3:46535ec6d8b1 | 26 | Serial_1.baud(9600); |
AlexQian | 3:46535ec6d8b1 | 27 | Serial_1.printf("Init\n"); |
AlexQian | 3:46535ec6d8b1 | 28 | mpu6050.Init(); |
AlexQian | 3:46535ec6d8b1 | 29 | |
AlexQian | 3:46535ec6d8b1 | 30 | nrf24_PB_15.powerUp(); |
AlexQian | 3:46535ec6d8b1 | 31 | nrf24_PB_15.setTransferSize( NRF24_TRANSFER_SIZE ); |
AlexQian | 3:46535ec6d8b1 | 32 | nrf24_PB_15.setReceiveMode(); |
AlexQian | 3:46535ec6d8b1 | 33 | nrf24_PB_15.setRxAddress(12345ull); |
AlexQian | 3:46535ec6d8b1 | 34 | nrf24_PB_15.setTxAddress(12345ull); |
AlexQian | 3:46535ec6d8b1 | 35 | nrf24_PB_15.enable(); |
AlexQian | 3:46535ec6d8b1 | 36 | |
AlexQian | 3:46535ec6d8b1 | 37 | |
AlexQian | 3:46535ec6d8b1 | 38 | |
AlexQian | 3:46535ec6d8b1 | 39 | CycleCount = 0; |
AlexQian | 3:46535ec6d8b1 | 40 | ReceiveCount = 0; |
AlexQian | 3:46535ec6d8b1 | 41 | Serial_1.printf("Ready to Receive\n"); |
AlexQian | 3:46535ec6d8b1 | 42 | while (true) { |
AlexQian | 3:46535ec6d8b1 | 43 | CycleCount = CycleCount + 1; |
AlexQian | 3:46535ec6d8b1 | 44 | |
AlexQian | 3:46535ec6d8b1 | 45 | mpu6050.receiveData(&Yaw,&Pitch,&Roll); //读取传感器姿态 |
AlexQian | 3:46535ec6d8b1 | 46 | Serial_1.printf("Sensor Data: Yaw, Pitch, Roll: %.2f %.2f %.2f\n\r", Yaw, Pitch, Roll); |
AlexQian | 3:46535ec6d8b1 | 47 | |
AlexQian | 3:46535ec6d8b1 | 48 | if (nrf24_PB_15.readable()) { |
AlexQian | 3:46535ec6d8b1 | 49 | ReceiveCount = ReceiveCount + 1; |
AlexQian | 3:46535ec6d8b1 | 50 | myDigitalOutPA_7.write((ReceiveCount % 2 == 0)); |
AlexQian | 3:46535ec6d8b1 | 51 | nrf24_PB_15.read( NRF24L01P_PIPE_P0, rxdata, NRF24_TRANSFER_SIZE); |
AlexQian | 3:46535ec6d8b1 | 52 | if (rxdata[0] == 82) { |
AlexQian | 3:46535ec6d8b1 | 53 | Power_t = rxdata[1]; |
AlexQian | 3:46535ec6d8b1 | 54 | Yaw_t = rxdata[2] - 45; |
AlexQian | 3:46535ec6d8b1 | 55 | Pitch_t = rxdata[3] - 45; |
AlexQian | 3:46535ec6d8b1 | 56 | Roll_t = rxdata[4] - 45; |
AlexQian | 3:46535ec6d8b1 | 57 | Serial_1.printf("Target: Power=%d, Yaw=%d, Pitch=%d, Roll=%d \n",Power_t,Yaw_t,Pitch_t,Roll_t); |
AlexQian | 3:46535ec6d8b1 | 58 | } |
AlexQian | 3:46535ec6d8b1 | 59 | } |
AlexQian | 3:46535ec6d8b1 | 60 | myDigitalOutPA_6.write((CycleCount % 2 == 0)); |
AlexQian | 3:46535ec6d8b1 | 61 | wait(0.01); |
Owen | 0:a51a6e7da590 | 62 | } |
wanzq | 2:4704fdd9ef91 | 63 | |
AlexQian | 3:46535ec6d8b1 | 64 | } |