四轴

Dependencies:   BufferedSerial SoftPWM mbed send

Fork of send_copy by aurora moon

Revision:
4:876bfa91934c
Parent:
3:61afd8d17063
Child:
5:683542d82dd7
--- a/main.cpp	Wed Oct 11 06:29:40 2017 +0000
+++ b/main.cpp	Wed Nov 08 06:24:17 2017 +0000
@@ -1,78 +1,100 @@
 #include "mbed.h"
 #include "nRF24L01P.h"
-
-Serial pc(USBTX, USBRX, 115200); // tx, rx
+#include "SoftPWM.h"
+#define JY901transfersize 11
+#define TRANSFER_SIZE   11
+#include"JY901.h"
+#define pwm_period 1
+int flagg = 0;
 
-//                 mosi, miso, sck, csn, ce, irq
-nRF24L01P my_nrf24l01p(D4, D5, D3, D7, D8, D6);
-
-DigitalOut myled1(LED1);
-DigitalOut myled2(LED2);
+char targetdata[3];
+char receivedata[4];
+float change[4];
+int receivedatacnt = 0;
+SoftPWM PWM1(PA_2),PWM2(PA_3),PWM3(PA_6),PWM4(PA_7),PWM5(PB_0),PWM6(PB_1);
 
-int main() {
-    
-    pc.printf("init\r\n");
+int rollx=0,rolly=0,rollz=0,drx,dry,drz,ax,ay,az;
+float kp,ki,kd;
+void pwm_init()
+{
+    PWM1.period_ms(pwm_period);
+    PWM2.period_ms(pwm_period);
+    PWM3.period_ms(pwm_period);
+    PWM4.period_ms(pwm_period);
+    PWM5.period_ms(pwm_period);
+    PWM6.period_ms(pwm_period);
+    PWM1=0.0;PWM2=0.0;PWM3=0.0;PWM4=0.0;PWM5=0.0;PWM6=0.0;
+}
 
-// The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
-//  "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
-//  only handles 4 byte transfers in the ATMega code.
-#define TRANSFER_SIZE   4
-
-    char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
+JY901 _JY901(PA_9,PA_10);
+//nRF24L01P my_nrf24l01p(PB_15, PB_14, PB_13, PC_1, PC_2, PC_0);
+char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
+    int JY901cnt = 0;
     int txDataCnt = 0;
     int rxDataCnt = 0;
-
-    my_nrf24l01p.powerUp();
-
-    // Display the (default) setup of the nRF24L01+ chip
-    pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
-    pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
-    pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
-    pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
-    pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
-
-    pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
-
-    my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
+void transferdata(){
+    _JY901.receiveData();
+    float acc[3];
+            _JY901.getAttitude(acc[0], acc[1], acc[2]);
+           PWM1=0.2;//0.13+0.13*(float)rollx/32767.0;
+        PWM2=0.4;//0.13-0.13*(float)rolly/32767.0;
+        PWM3=0.1-0.1*acc[0]/180;
+}
 
-    my_nrf24l01p.setReceiveMode();
-    my_nrf24l01p.enable();
-
-    while (1) {
-
-        // If we've received anything over the host serial link...
-        if ( pc.readable() ) {
-
-            // ...add it to the transmit buffer
-            txData[txDataCnt++] = pc.getc();
-
-            // If the transmit buffer is full
-            if ( txDataCnt >= sizeof( txData ) ) {
+int PIDcontrol(char *targetdata,char *data,float *change){
+    return 1;
+}
 
-                // Send the transmitbuffer via the nRF24L01+
-                my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
-
-                txDataCnt = 0;
-            }
-
-            // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
-            myled1 = !myled1;
-        }
-
-        // If we've received anything in the nRF24L01+...
-        if ( my_nrf24l01p.readable() ) {
+void update_roll()
+{
+   // char *s;
+    /*char data[JY901transfersize];
+    char flag =0,flag2 = 0;
+    int q=1;
+    while(q < 1000){
+        q++;
+        switch(flag){
+            case 0:
+                flag2=JY901.getc();
+                if(flag2==0x55) flag++;
+            break;
+            case 1:
+                flag2=JY901.getc();
+                if(flag2 == 0x53){
+                    flag++;
+                    data[0] = 0x55;
+                    data[1] = flag2;
+                    }
+                else flag--;
+            break;
+            case 2:
+                for(int i = 2;i != JY901transfersize;i++)
+                    data[i] = JY901.getc();
+                q = 1001;
+            break;
+        }        
+    }*/
 
-            // ...read the data into the receive buffer
-            rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
-
-            // Display the receive buffer contents via the host serial link
-            for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
+   // rollx+=drx;
+   // rolly+=dry;
+    //rollz+=drz;
+   // sprintf(s,"%f",rollx*180/32767);
+   // my_nrf24l01p.write( NRF24L01P_PIPE_P0,s,11);
+}
+int main() { 
+  //  my_nrf24l01p.powerUp();  
+   // my_nrf24l01p.setTransferSize( 11);
+   // my_nrf24l01p.setReceiveMode();
+  //  my_nrf24l01p.enable();  
 
-                pc.putc( rxData[i] );
-            }
+    Ticker time;
+    time.attach(&transferdata,0.1); 
+    while(1){
+   
+       // pc.printf("succes");
+ 
+    
+    } 
+       
+}
 
-            // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
-            myled2 = !myled2;
-        }
-    }
-}