PSL_2021 / Mbed OS prog_robot_1_Eve_v4

Dependencies:   mbed-os nRF24L01P

Revision:
3:793d3386768b
Parent:
2:e309f19062b7
Child:
4:4fae82a1be60
--- a/main.cpp	Fri Dec 04 08:16:27 2020 +0000
+++ b/main.cpp	Fri Apr 23 12:36:02 2021 +0000
@@ -1,82 +1,101 @@
+/*
+RobotCup ENS Paris Saclay 2020-2021
+Team FC Furious
+Code by Luc DERRIEN
+
+Programme communication PC-Robot
+Point de vue : PC
+micro controleur : L475
+
+envoi d'une chaine de caractère de taille TRANSFER_SIZE = 17
+*/
+
 #include "mbed.h"
+#include "string.h"
 #include "nRF24L01P.h"
+#include "MX12.h"
+#include "moteur.h"
 
-Serial pc(USBTX, USBRX); // tx, rx
+char text[50];
+
+// hacheur
+PwmOut hacheur(D15);
+
+// commande moteur
+
+UnbufferedSerial pc_serie(USBTX,USBRX,115200);
+MX12 servo(PC_4,PC_5,115200);
+
+//Serial pc(USBTX, USBRX); // tx, rx
 
 //Définition des E/S de la liaison SPI vers le module nRF24L01+
-nRF24L01P my_nrf24l01p(D11, D12, D13, A3, D9, A0);    // mosi, miso, sck, csn, ce, irq
+nRF24L01P my_nrf24l01p(D11, D12, D13, D9, D2, D1);    // mosi, miso, sck, csn, ce, irq
 
-DigitalOut myled1(D4);
-DigitalOut myled2(D5);
-
-int main() {
+int main()
+{
 
 // Nombre de caractères transmis à chaque envoi
-#define TRANSFER_SIZE   4
+#define TRANSFER_SIZE   20
+
+    hacheur.period_ms(1.0f);
 
     char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
     int txDataCnt = 0;
     int rxDataCnt = 0;
-    
-    pc.baud(115200); // Débit de la liaison série PC
+    int id_robot, v_tangent, v_normale, omega_robot, spiner_bool, v_tir ;
+    float v_tr, v_nr, omega_rr;
+
+    // pc.baud(115200); // Débit de la liaison série PC
+
 
     my_nrf24l01p.powerUp();
-    
+
     my_nrf24l01p.setRfFrequency(2416); // Définition de la fréquence du canal d'E/R
     my_nrf24l01p.setAirDataRate(NRF24L01P_DATARATE_1_MBPS); // Définition du débit de la communication RF
-    
-
-    // Affichage de l'état de la configuration du nRF24L01+ 
-    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 );
     my_nrf24l01p.setReceiveMode();
     my_nrf24l01p.enable();
-    
 
+    cmd_moteur(0,0,0);
+    hacheur.write(0.0f);
     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 ) ) {
-
-                // 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+2...
         if ( my_nrf24l01p.readable() ) {
 
             // ...read the data into the receive buffer
             rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
+            if(sscanf(rxData, "%d,%d,%d,%d,%d,%d",&id_robot,&v_tangent,&v_normale,&omega_robot,&spiner_bool,&v_tir)==6) { // lecture et parsing de la chaîne
 
-            // Display the receive buffer contents via the host serial link
-            for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
+                if (v_tangent > 999) {
+                    v_tangent = - (v_tangent - 1000);
+                }
+
+                if (v_normale > 999) {
+                    v_normale = - (v_normale - 1000);
+                }
 
-                pc.putc( rxData[i] );
+                if (omega_robot > 999) {
+                    omega_robot = - (omega_robot - 1000);
+                }
+                pc_serie.write(text,strlen(text));
             }
+            rxDataCnt = 0;
 
-            // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
-            myled2 = !myled2;
+            // commande moteur
+            v_tr=v_tangent*0.8/999;
+            v_nr=v_normale*0.8/999;
+            omega_rr=omega_robot*2.9/999;
+            cmd_moteur(v_tr,v_nr,omega_rr);
+
+            // hacheur dribbleur
+            if ( spiner_bool )
+                hacheur.write(0.3f);
+            else
+                hacheur.write(0.0f);
+
         }
+
     }
 }