Qian Yuyang
/
Fly_test
NRF+MPU6050
main.cpp@4:1b985e622d26, 2019-03-20 (annotated)
- Committer:
- AlexQian
- Date:
- Wed Mar 20 10:44:31 2019 +0000
- Revision:
- 4:1b985e622d26
- Parent:
- 3:46535ec6d8b1
Fly_test
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" |
AlexQian | 3:46535ec6d8b1 | 4 | #include <string> |
AlexQian | 3:46535ec6d8b1 | 5 | typedef bool boolean; |
AlexQian | 3:46535ec6d8b1 | 6 | typedef std::string String; |
AlexQian | 4:1b985e622d26 | 7 | #include "nRF24L01P.h" |
AlexQian | 4:1b985e622d26 | 8 | #include "MPU6050.h" |
Owen | 0:a51a6e7da590 | 9 | |
AlexQian | 3:46535ec6d8b1 | 10 | char rxdata[12] ; |
AlexQian | 4:1b985e622d26 | 11 | float Yaw; |
AlexQian | 4:1b985e622d26 | 12 | float Pitch; |
AlexQian | 4:1b985e622d26 | 13 | float Roll; |
AlexQian | 4:1b985e622d26 | 14 | int Power; |
AlexQian | 3:46535ec6d8b1 | 15 | int Yaw_t; |
AlexQian | 3:46535ec6d8b1 | 16 | int Pitch_t; |
AlexQian | 3:46535ec6d8b1 | 17 | int Roll_t; |
AlexQian | 3:46535ec6d8b1 | 18 | long CycleCount; |
AlexQian | 3:46535ec6d8b1 | 19 | long ReceiveCount; |
Owen | 0:a51a6e7da590 | 20 | |
AlexQian | 3:46535ec6d8b1 | 21 | #define NRF24_TRANSFER_SIZE 12 |
AlexQian | 3:46535ec6d8b1 | 22 | nRF24L01P nrf24_PB_15(PB_15,PB_14,PB_13,PB_6,PB_5,PB_7); |
AlexQian | 4:1b985e622d26 | 23 | Serial Serial_2(PA_2,PA_3); |
AlexQian | 4:1b985e622d26 | 24 | mpu6050 mpu6050_I2C_1(PB_9,PB_8); |
AlexQian | 3:46535ec6d8b1 | 25 | DigitalOut myDigitalOutPA_7(PA_7); |
AlexQian | 3:46535ec6d8b1 | 26 | DigitalOut myDigitalOutPA_6(PA_6); |
AlexQian | 4:1b985e622d26 | 27 | |
Owen | 0:a51a6e7da590 | 28 | int main() { |
AlexQian | 3:46535ec6d8b1 | 29 | nrf24_PB_15.powerUp(); |
AlexQian | 3:46535ec6d8b1 | 30 | nrf24_PB_15.setTransferSize( NRF24_TRANSFER_SIZE ); |
AlexQian | 3:46535ec6d8b1 | 31 | nrf24_PB_15.setReceiveMode(); |
AlexQian | 3:46535ec6d8b1 | 32 | nrf24_PB_15.setRxAddress(12345ull); |
AlexQian | 3:46535ec6d8b1 | 33 | nrf24_PB_15.setTxAddress(12345ull); |
AlexQian | 3:46535ec6d8b1 | 34 | nrf24_PB_15.enable(); |
AlexQian | 3:46535ec6d8b1 | 35 | |
AlexQian | 4:1b985e622d26 | 36 | Serial_2.baud(9600); |
AlexQian | 3:46535ec6d8b1 | 37 | |
AlexQian | 4:1b985e622d26 | 38 | mpu6050_I2C_1.Init(); |
AlexQian | 3:46535ec6d8b1 | 39 | |
AlexQian | 3:46535ec6d8b1 | 40 | CycleCount = 0; |
AlexQian | 3:46535ec6d8b1 | 41 | ReceiveCount = 0; |
AlexQian | 3:46535ec6d8b1 | 42 | while (true) { |
AlexQian | 3:46535ec6d8b1 | 43 | CycleCount = CycleCount + 1; |
AlexQian | 4:1b985e622d26 | 44 | mpu6050_I2C_1.receiveData(&Yaw,&Pitch,&Roll); |
AlexQian | 4:1b985e622d26 | 45 | Serial_2.printf("Yaw=%.2f, Pitch=%.2f, Roll=%.2f \n",Yaw,Pitch,Roll); |
AlexQian | 3:46535ec6d8b1 | 46 | if (nrf24_PB_15.readable()) { |
AlexQian | 3:46535ec6d8b1 | 47 | ReceiveCount = ReceiveCount + 1; |
AlexQian | 3:46535ec6d8b1 | 48 | myDigitalOutPA_7.write((ReceiveCount % 2 == 0)); |
AlexQian | 3:46535ec6d8b1 | 49 | nrf24_PB_15.read( NRF24L01P_PIPE_P0, rxdata, NRF24_TRANSFER_SIZE); |
AlexQian | 3:46535ec6d8b1 | 50 | if (rxdata[0] == 82) { |
AlexQian | 4:1b985e622d26 | 51 | Power = rxdata[1]; |
AlexQian | 3:46535ec6d8b1 | 52 | Yaw_t = rxdata[2] - 45; |
AlexQian | 3:46535ec6d8b1 | 53 | Pitch_t = rxdata[3] - 45; |
AlexQian | 3:46535ec6d8b1 | 54 | Roll_t = rxdata[4] - 45; |
AlexQian | 4:1b985e622d26 | 55 | Serial_2.printf("Power=%d, Yaw=%d, Pitch=%d, Roll=%d \n",Power,Yaw_t,Pitch_t,Roll_t); |
AlexQian | 3:46535ec6d8b1 | 56 | } |
AlexQian | 3:46535ec6d8b1 | 57 | } |
AlexQian | 3:46535ec6d8b1 | 58 | myDigitalOutPA_6.write((CycleCount % 2 == 0)); |
AlexQian | 4:1b985e622d26 | 59 | wait(0.05); |
Owen | 0:a51a6e7da590 | 60 | } |
wanzq | 2:4704fdd9ef91 | 61 | |
AlexQian | 3:46535ec6d8b1 | 62 | } |