![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
1223
Dependencies: BufferedSerial SoftPWM mbed send
Fork of nRF24L01P_Hello_World by
main.cpp@4:876bfa91934c, 2017-11-08 (annotated)
- Committer:
- accelerator225
- Date:
- Wed Nov 08 06:24:17 2017 +0000
- Revision:
- 4:876bfa91934c
- Parent:
- 3:61afd8d17063
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Owen | 0:a51a6e7da590 | 1 | #include "mbed.h" |
Owen | 0:a51a6e7da590 | 2 | #include "nRF24L01P.h" |
accelerator225 | 4:876bfa91934c | 3 | #include "SoftPWM.h" |
accelerator225 | 4:876bfa91934c | 4 | #define JY901transfersize 11 |
accelerator225 | 4:876bfa91934c | 5 | #define TRANSFER_SIZE 11 |
accelerator225 | 4:876bfa91934c | 6 | #include"JY901.h" |
accelerator225 | 4:876bfa91934c | 7 | #define pwm_period 1 |
accelerator225 | 4:876bfa91934c | 8 | int flagg = 0; |
Owen | 0:a51a6e7da590 | 9 | |
accelerator225 | 4:876bfa91934c | 10 | char targetdata[3]; |
accelerator225 | 4:876bfa91934c | 11 | char receivedata[4]; |
accelerator225 | 4:876bfa91934c | 12 | float change[4]; |
accelerator225 | 4:876bfa91934c | 13 | int receivedatacnt = 0; |
accelerator225 | 4:876bfa91934c | 14 | SoftPWM PWM1(PA_2),PWM2(PA_3),PWM3(PA_6),PWM4(PA_7),PWM5(PB_0),PWM6(PB_1); |
Owen | 0:a51a6e7da590 | 15 | |
accelerator225 | 4:876bfa91934c | 16 | int rollx=0,rolly=0,rollz=0,drx,dry,drz,ax,ay,az; |
accelerator225 | 4:876bfa91934c | 17 | float kp,ki,kd; |
accelerator225 | 4:876bfa91934c | 18 | void pwm_init() |
accelerator225 | 4:876bfa91934c | 19 | { |
accelerator225 | 4:876bfa91934c | 20 | PWM1.period_ms(pwm_period); |
accelerator225 | 4:876bfa91934c | 21 | PWM2.period_ms(pwm_period); |
accelerator225 | 4:876bfa91934c | 22 | PWM3.period_ms(pwm_period); |
accelerator225 | 4:876bfa91934c | 23 | PWM4.period_ms(pwm_period); |
accelerator225 | 4:876bfa91934c | 24 | PWM5.period_ms(pwm_period); |
accelerator225 | 4:876bfa91934c | 25 | PWM6.period_ms(pwm_period); |
accelerator225 | 4:876bfa91934c | 26 | PWM1=0.0;PWM2=0.0;PWM3=0.0;PWM4=0.0;PWM5=0.0;PWM6=0.0; |
accelerator225 | 4:876bfa91934c | 27 | } |
Owen | 0:a51a6e7da590 | 28 | |
accelerator225 | 4:876bfa91934c | 29 | JY901 _JY901(PA_9,PA_10); |
accelerator225 | 4:876bfa91934c | 30 | //nRF24L01P my_nrf24l01p(PB_15, PB_14, PB_13, PC_1, PC_2, PC_0); |
accelerator225 | 4:876bfa91934c | 31 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
accelerator225 | 4:876bfa91934c | 32 | int JY901cnt = 0; |
Owen | 0:a51a6e7da590 | 33 | int txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 34 | int rxDataCnt = 0; |
accelerator225 | 4:876bfa91934c | 35 | void transferdata(){ |
accelerator225 | 4:876bfa91934c | 36 | _JY901.receiveData(); |
accelerator225 | 4:876bfa91934c | 37 | float acc[3]; |
accelerator225 | 4:876bfa91934c | 38 | _JY901.getAttitude(acc[0], acc[1], acc[2]); |
accelerator225 | 4:876bfa91934c | 39 | PWM1=0.2;//0.13+0.13*(float)rollx/32767.0; |
accelerator225 | 4:876bfa91934c | 40 | PWM2=0.4;//0.13-0.13*(float)rolly/32767.0; |
accelerator225 | 4:876bfa91934c | 41 | PWM3=0.1-0.1*acc[0]/180; |
accelerator225 | 4:876bfa91934c | 42 | } |
Owen | 0:a51a6e7da590 | 43 | |
accelerator225 | 4:876bfa91934c | 44 | int PIDcontrol(char *targetdata,char *data,float *change){ |
accelerator225 | 4:876bfa91934c | 45 | return 1; |
accelerator225 | 4:876bfa91934c | 46 | } |
Owen | 0:a51a6e7da590 | 47 | |
accelerator225 | 4:876bfa91934c | 48 | void update_roll() |
accelerator225 | 4:876bfa91934c | 49 | { |
accelerator225 | 4:876bfa91934c | 50 | // char *s; |
accelerator225 | 4:876bfa91934c | 51 | /*char data[JY901transfersize]; |
accelerator225 | 4:876bfa91934c | 52 | char flag =0,flag2 = 0; |
accelerator225 | 4:876bfa91934c | 53 | int q=1; |
accelerator225 | 4:876bfa91934c | 54 | while(q < 1000){ |
accelerator225 | 4:876bfa91934c | 55 | q++; |
accelerator225 | 4:876bfa91934c | 56 | switch(flag){ |
accelerator225 | 4:876bfa91934c | 57 | case 0: |
accelerator225 | 4:876bfa91934c | 58 | flag2=JY901.getc(); |
accelerator225 | 4:876bfa91934c | 59 | if(flag2==0x55) flag++; |
accelerator225 | 4:876bfa91934c | 60 | break; |
accelerator225 | 4:876bfa91934c | 61 | case 1: |
accelerator225 | 4:876bfa91934c | 62 | flag2=JY901.getc(); |
accelerator225 | 4:876bfa91934c | 63 | if(flag2 == 0x53){ |
accelerator225 | 4:876bfa91934c | 64 | flag++; |
accelerator225 | 4:876bfa91934c | 65 | data[0] = 0x55; |
accelerator225 | 4:876bfa91934c | 66 | data[1] = flag2; |
accelerator225 | 4:876bfa91934c | 67 | } |
accelerator225 | 4:876bfa91934c | 68 | else flag--; |
accelerator225 | 4:876bfa91934c | 69 | break; |
accelerator225 | 4:876bfa91934c | 70 | case 2: |
accelerator225 | 4:876bfa91934c | 71 | for(int i = 2;i != JY901transfersize;i++) |
accelerator225 | 4:876bfa91934c | 72 | data[i] = JY901.getc(); |
accelerator225 | 4:876bfa91934c | 73 | q = 1001; |
accelerator225 | 4:876bfa91934c | 74 | break; |
accelerator225 | 4:876bfa91934c | 75 | } |
accelerator225 | 4:876bfa91934c | 76 | }*/ |
Owen | 0:a51a6e7da590 | 77 | |
accelerator225 | 4:876bfa91934c | 78 | // rollx+=drx; |
accelerator225 | 4:876bfa91934c | 79 | // rolly+=dry; |
accelerator225 | 4:876bfa91934c | 80 | //rollz+=drz; |
accelerator225 | 4:876bfa91934c | 81 | // sprintf(s,"%f",rollx*180/32767); |
accelerator225 | 4:876bfa91934c | 82 | // my_nrf24l01p.write( NRF24L01P_PIPE_P0,s,11); |
accelerator225 | 4:876bfa91934c | 83 | } |
accelerator225 | 4:876bfa91934c | 84 | int main() { |
accelerator225 | 4:876bfa91934c | 85 | // my_nrf24l01p.powerUp(); |
accelerator225 | 4:876bfa91934c | 86 | // my_nrf24l01p.setTransferSize( 11); |
accelerator225 | 4:876bfa91934c | 87 | // my_nrf24l01p.setReceiveMode(); |
accelerator225 | 4:876bfa91934c | 88 | // my_nrf24l01p.enable(); |
Owen | 0:a51a6e7da590 | 89 | |
accelerator225 | 4:876bfa91934c | 90 | Ticker time; |
accelerator225 | 4:876bfa91934c | 91 | time.attach(&transferdata,0.1); |
accelerator225 | 4:876bfa91934c | 92 | while(1){ |
accelerator225 | 4:876bfa91934c | 93 | |
accelerator225 | 4:876bfa91934c | 94 | // pc.printf("succes"); |
accelerator225 | 4:876bfa91934c | 95 | |
accelerator225 | 4:876bfa91934c | 96 | |
accelerator225 | 4:876bfa91934c | 97 | } |
accelerator225 | 4:876bfa91934c | 98 | |
accelerator225 | 4:876bfa91934c | 99 | } |
Owen | 0:a51a6e7da590 | 100 |