Terrabots / Mbed 2 deprecated DUMP_TRUCK_SPR2017

Dependencies:   Tracker mbed MotorV2 SRF05 nRF24L01P

Fork of DUMP_TRUCK_TEST_V1 by Terrabots

Files at this revision

API Documentation at this revision

Comitter:
simplyellow
Date:
Thu Apr 13 17:32:44 2017 +0000
Parent:
12:502cf4fc6d98
Child:
14:fd6090cddc2e
Commit message:
.

Changed in this revision

DumpTruck.cpp Show annotated file Show diff for this revision Revisions of this file
DumpTruck.h Show annotated file Show diff for this revision Revisions of this file
commands.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/DumpTruck.cpp	Thu Mar 30 17:27:21 2017 +0000
+++ b/DumpTruck.cpp	Thu Apr 13 17:32:44 2017 +0000
@@ -1,21 +1,27 @@
 #include "DumpTruck.h"
 
+
 DumpTruck::DumpTruck(int truckId) : truckNumber(truckId) {
     truckNumber = truckId;
         //track = new Tracker(p29, p30, p15);
     // PWM: 21, 22, 23
     // DIR: 27, 28, 29
     frontMotor = new Motor(p23, p28);
-        //turnMotor = new Motor(p22, p29);
-        //bedMotor = new Motor(p23, p28);
+    turnMotor = new Motor(p22, p29);
+    bedMotor = new Motor(p23, p28);
     //bed = new IMU(@@@@@@@);
     srf = new SRF05(p15, p16); //trigger, echo.
     tooClose = false;
-    
+
     nrf = new nRF24L01P(p5, p6, p7, p8, p9, p10); // transceiver
     //transceiver set up, similar to one on base station code
     txDataCnt = 0;
     rxDataCnt = 0;
+    received = false;
+    
+    void (*commands[2]);
+    commands[0] = DumpTruck::stopBed;
+    commands[1] = DumpTruck::stopDrive;
 }
 
 void DumpTruck::startComms() {
@@ -28,62 +34,104 @@
     nrf->setTransferSize( TRANSFER_SIZE );
     nrf->setReceiveMode();
     nrf->enable();
-    received = false;
+    
+    // fill acked array
+    for(int i = 0; i < TRANSFER_SIZE; i++) {
+        acked[i] = '0';
+        nacked[i] = '1';
+    }
+}
+
+void DumpTruck::sendAck() {
+    printf("SEND acked\n\r");
+    wait(1);
+    nrf->write(NRF24L01P_PIPE_P0, acked, TRANSFER_SIZE);
+    printf("SENT\n\r");
+}
+
+void DumpTruck::sendNack() {
+    printf("SEND nacked\n\r");
+    wait(1);
+    nrf->write(NRF24L01P_PIPE_P0, nacked, TRANSFER_SIZE);
+    printf("SENT\n\r");
 }
 
 void DumpTruck::getCommand() {
-    received = false;
     //called at the end of each executable command
-    //print statement saying dump truck is ready to receive
-        //have print statement appear in base station code
     //dump truck transceiver sends a code to base station, opening the channel
-        //code is all 0's
-            for(int i = 0; i < TRANSFER_SIZE; i++) {
-                txData[i] = 0;
-            }
-            nrf->write(NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE);
-        //possibly implement kill switch/stop current action
-    //...
+    //code is all 0's
+    received = false;
+    //acked to receive command
+    sendAck();
+    //possibly implement kill switch/stop current action
     //code for receiving
     while(!received) {
-        if (nrf->readable() ) {
+        if (nrf->readable()) {
+        // ...read the data into the receive buffer
             rxDataCnt = nrf->read(NRF24L01P_PIPE_P0, rxData, sizeof(rxData));
-            /*
-            either go char by char to determine command or switch statement
-            for ( int j = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
+            received = true; //after receiving command
+            printf("received command\n\r");
+            processCommand();
+        }
+    }
+}
 
-                pc.putc( rxData[j] );
-            }*/
-        }
-        //received = true after receiving command
+void DumpTruck::processCommand() {
+    //strcmp(rxData,"on")==0
+    //perhaps have an array of all commands..??
+        //maybe commands.h is just the array
+    //and the "on" becomes commands[k]?
+    // real time driving
+    switch(rxData) {
+        case 1:
+            sendAck();
+            printf("ack\n\r");
+            break;
+        default:
+            sendNack();
+            printf("nack\n\r");
+            break;
     }
-    //switch statement, sorting all commands
-    
+    getCommand();
 }
 
 void DumpTruck::reportData() {
     //should run last in main thread
-
 }
 
 void DumpTruck::driveDistance(float speed, float distance) {
     frontMotor->write(speed);
+    getCommand();
 }
 
 void DumpTruck::drive(float speed) {
     frontMotor->write(speed);
+    getCommand();
 }
 
 void DumpTruck::turn(float angle) {
+    getCommand();
     return;
 }
 
 void DumpTruck::moveBed(bool raise, float angle) {
+    getCommand();
     return;
 }
 
-void DumpTruck::stop() {
+void DumpTruck::stopDrive() {
     frontMotor->write(0);
+    getCommand();
+}
+
+void DumpTruck::stopTurn() {
+    turnMotor->write(0);
+    getCommand();
+}
+
+void DumpTruck::stopBed() {
+    bedMotor->write(0);
+    getCommand();
 }
 
 bool DumpTruck::detect() {
--- a/DumpTruck.h	Thu Mar 30 17:27:21 2017 +0000
+++ b/DumpTruck.h	Thu Apr 13 17:32:44 2017 +0000
@@ -17,6 +17,11 @@
 #include "nRF24L01P.h" //Transceiver library
 #include "SRF05.h"
 
+//using strings to process commands
+#include <string> 
+#include <stdio.h>
+using namespace std;
+
 #define TRANSFER_SIZE   8
 
 // ADD SWITCHES & ULTRASONIC SENSOR implementation
@@ -37,6 +42,23 @@
     DumpTruck(int truckId);
 
     /*
+    *   Process command
+    */
+    void processCommand();
+
+    /*
+    *   Send not acknowledge
+    */
+
+    void sendNack();
+
+    /*
+    *   Send acknowledge
+    */
+
+    void sendAck();
+    
+    /*
     *   Initialize transceiver.
     */
     void startComms();
@@ -71,7 +93,17 @@
     /*
     *   Stop driving.
     */
-    void stop();                            // all Motors
+    int stopDrive();                            // all Motors
+    
+    /*
+    *   Stop turning.
+    */
+    int stopTurn();                            // all Motors
+
+    /*
+    *   Stop bed.
+    */
+    int stopBed();                            // all Motors
 
     // ultrasonic functions
     /*
@@ -79,6 +111,13 @@
     *   proximity from objects.
     */
     bool detect();                          // returns bool for object too close
+    
+    int txDataCnt;
+    int rxDataCnt;
+    char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
+    char acked[TRANSFER_SIZE];
+    char nacked[TRANSFER_SIZE]; 
+    bool received;
 
 
 protected:
@@ -107,9 +146,9 @@
     bool tooClose;
     float proximity;
     
-    int txDataCnt;
+    /*int txDataCnt;
     int rxDataCnt;
     char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
-    bool received;
+    bool received;*/
 };
 #endif
--- a/commands.h	Thu Mar 30 17:27:21 2017 +0000
+++ b/commands.h	Thu Apr 13 17:32:44 2017 +0000
@@ -1,1 +1,3 @@
-#define DR
\ No newline at end of file
+#include "DumpTruck.h"
+
+string a("text");
\ No newline at end of file
--- a/main.cpp	Thu Mar 30 17:27:21 2017 +0000
+++ b/main.cpp	Thu Apr 13 17:32:44 2017 +0000
@@ -1,21 +1,18 @@
 #include "DumpTruck.h"
 DumpTruck dump(1);
+Serial pc(USBTX, USBRX);
 
 /*
 test code for driving and stopping when an object gets too close
 to the dump truck.
 */
 
-int main() {
-    
-    while(1) {        
-        printf("%f\r\n",(float)dump.detect());
-        if(dump.detect()) {
-            dump.stop();
-        } else {
-            dump.drive(0.5f);
-        }
-    }
-    
+int main() {   
+    dump.startComms();
+    dump.getCommand();
+    /*
+    for(int i = 0; i < dump.rxDataCnt; i++) {
+        pc.putc(dump.rxData[i]);
+    }*/ 
 }