Remote control code for https://developer.mbed.org/users/ivo_david_michelle/code/QuadTrio/
Dependencies: Analog_Joystick Sender mbed
Fork of ESE350-Whack-a-Mole by
rc.cpp@5:3ad830e95ffd, 2016-03-28 (annotated)
- Committer:
- ivo_david_michelle
- Date:
- Mon Mar 28 23:18:26 2016 +0000
- Revision:
- 5:3ad830e95ffd
- Child:
- 6:db985df4354b
revision commit march 28
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ivo_david_michelle | 5:3ad830e95ffd | 1 | #include "MRF24J40.h" |
ivo_david_michelle | 5:3ad830e95ffd | 2 | #include "sender.h" |
ivo_david_michelle | 5:3ad830e95ffd | 3 | #include "analog_joystick.h" |
ivo_david_michelle | 5:3ad830e95ffd | 4 | |
ivo_david_michelle | 5:3ad830e95ffd | 5 | #include <string> |
ivo_david_michelle | 5:3ad830e95ffd | 6 | |
ivo_david_michelle | 5:3ad830e95ffd | 7 | // RF tranceiver to link with handheld. |
ivo_david_michelle | 5:3ad830e95ffd | 8 | MRF24J40 mrf(p11, p12, p13, p14, p21); |
ivo_david_michelle | 5:3ad830e95ffd | 9 | |
ivo_david_michelle | 5:3ad830e95ffd | 10 | // LEDs you can treat these as variables (led2 = 1 will turn led2 on!) |
ivo_david_michelle | 5:3ad830e95ffd | 11 | DigitalOut led1(LED1); |
ivo_david_michelle | 5:3ad830e95ffd | 12 | DigitalOut led2(LED2); |
ivo_david_michelle | 5:3ad830e95ffd | 13 | DigitalOut led3(LED3); |
ivo_david_michelle | 5:3ad830e95ffd | 14 | DigitalOut led4(LED4); |
ivo_david_michelle | 5:3ad830e95ffd | 15 | |
ivo_david_michelle | 5:3ad830e95ffd | 16 | // Timer |
ivo_david_michelle | 5:3ad830e95ffd | 17 | Timer timer; |
ivo_david_michelle | 5:3ad830e95ffd | 18 | |
ivo_david_michelle | 5:3ad830e95ffd | 19 | // Serial port for showing RX data. |
ivo_david_michelle | 5:3ad830e95ffd | 20 | Serial pc(USBTX, USBRX); |
ivo_david_michelle | 5:3ad830e95ffd | 21 | |
ivo_david_michelle | 5:3ad830e95ffd | 22 | // Used for sending and receiving |
ivo_david_michelle | 5:3ad830e95ffd | 23 | char txBuffer[128]; |
ivo_david_michelle | 5:3ad830e95ffd | 24 | char rxBuffer[128]; |
ivo_david_michelle | 5:3ad830e95ffd | 25 | int rxLen; |
ivo_david_michelle | 5:3ad830e95ffd | 26 | LocalFileSystem local("local"); |
ivo_david_michelle | 5:3ad830e95ffd | 27 | |
ivo_david_michelle | 5:3ad830e95ffd | 28 | //***************** send and receive methods *****************// |
ivo_david_michelle | 5:3ad830e95ffd | 29 | /* |
ivo_david_michelle | 5:3ad830e95ffd | 30 | void wait_for_ping(uint8_t length) { |
ivo_david_michelle | 5:3ad830e95ffd | 31 | int receive = 0; |
ivo_david_michelle | 5:3ad830e95ffd | 32 | uint8_t *rssi = 0; |
ivo_david_michelle | 5:3ad830e95ffd | 33 | |
ivo_david_michelle | 5:3ad830e95ffd | 34 | while (receive == 0) { |
ivo_david_michelle | 5:3ad830e95ffd | 35 | receive = rf_receive_rssi(rxBuffer, rssi, length); |
ivo_david_michelle | 5:3ad830e95ffd | 36 | } |
ivo_david_michelle | 5:3ad830e95ffd | 37 | } |
ivo_david_michelle | 5:3ad830e95ffd | 38 | */ |
ivo_david_michelle | 5:3ad830e95ffd | 39 | |
ivo_david_michelle | 5:3ad830e95ffd | 40 | |
ivo_david_michelle | 5:3ad830e95ffd | 41 | //***************** main loop *****************// |
ivo_david_michelle | 5:3ad830e95ffd | 42 | |
ivo_david_michelle | 5:3ad830e95ffd | 43 | int main (void) |
ivo_david_michelle | 5:3ad830e95ffd | 44 | { |
ivo_david_michelle | 5:3ad830e95ffd | 45 | uint8_t channel = 3; |
ivo_david_michelle | 5:3ad830e95ffd | 46 | uint8_t iterations = 0; |
ivo_david_michelle | 5:3ad830e95ffd | 47 | uint8_t successCounter = 0; |
ivo_david_michelle | 5:3ad830e95ffd | 48 | uint8_t rssiBuffer[iterations]; |
ivo_david_michelle | 5:3ad830e95ffd | 49 | |
ivo_david_michelle | 5:3ad830e95ffd | 50 | char *byteTransmit = "!"; // TODO: change this! |
ivo_david_michelle | 5:3ad830e95ffd | 51 | |
ivo_david_michelle | 5:3ad830e95ffd | 52 | //Set the Channel. 0 is default, 15 is max |
ivo_david_michelle | 5:3ad830e95ffd | 53 | mrf.SetChannel(channel); |
ivo_david_michelle | 5:3ad830e95ffd | 54 | |
ivo_david_michelle | 5:3ad830e95ffd | 55 | //Start the timer |
ivo_david_michelle | 5:3ad830e95ffd | 56 | timer.start(); |
ivo_david_michelle | 5:3ad830e95ffd | 57 | |
ivo_david_michelle | 5:3ad830e95ffd | 58 | pc.printf("START\r\n"); |
ivo_david_michelle | 5:3ad830e95ffd | 59 | |
ivo_david_michelle | 5:3ad830e95ffd | 60 | FILE *fp = fopen("/local/out.txt\r\n", "w"); |
ivo_david_michelle | 5:3ad830e95ffd | 61 | if (fp == NULL) { |
ivo_david_michelle | 5:3ad830e95ffd | 62 | pc.printf("error opening file\r\n"); |
ivo_david_michelle | 5:3ad830e95ffd | 63 | return 0; |
ivo_david_michelle | 5:3ad830e95ffd | 64 | } |
ivo_david_michelle | 5:3ad830e95ffd | 65 | |
ivo_david_michelle | 5:3ad830e95ffd | 66 | int count = 15; |
ivo_david_michelle | 5:3ad830e95ffd | 67 | while (count > 0) { |
ivo_david_michelle | 5:3ad830e95ffd | 68 | count--; |
ivo_david_michelle | 5:3ad830e95ffd | 69 | pc.printf("yaw: %d\r\n", read_yaw()); |
ivo_david_michelle | 5:3ad830e95ffd | 70 | pc.printf("pitch: %d\r\n", read_pitch()); |
ivo_david_michelle | 5:3ad830e95ffd | 71 | pc.printf("roll: %d\r\n", read_roll()); |
ivo_david_michelle | 5:3ad830e95ffd | 72 | pc.printf("thrust: %d\r\n", read_thrust()); |
ivo_david_michelle | 5:3ad830e95ffd | 73 | |
ivo_david_michelle | 5:3ad830e95ffd | 74 | wait(0.2); |
ivo_david_michelle | 5:3ad830e95ffd | 75 | } |
ivo_david_michelle | 5:3ad830e95ffd | 76 | |
ivo_david_michelle | 5:3ad830e95ffd | 77 | |
ivo_david_michelle | 5:3ad830e95ffd | 78 | // send_three_way_handshake(); |
ivo_david_michelle | 5:3ad830e95ffd | 79 | |
ivo_david_michelle | 5:3ad830e95ffd | 80 | for (int i = 0; i < iterations; i++) { |
ivo_david_michelle | 5:3ad830e95ffd | 81 | strcpy(txBuffer, byteTransmit); |
ivo_david_michelle | 5:3ad830e95ffd | 82 | rf_send(mrf, txBuffer, strlen(txBuffer) + 1); |
ivo_david_michelle | 5:3ad830e95ffd | 83 | pc.printf("RC Sent: %s \r\n", txBuffer); |
ivo_david_michelle | 5:3ad830e95ffd | 84 | |
ivo_david_michelle | 5:3ad830e95ffd | 85 | wait(0.2); |
ivo_david_michelle | 5:3ad830e95ffd | 86 | } |
ivo_david_michelle | 5:3ad830e95ffd | 87 | |
ivo_david_michelle | 5:3ad830e95ffd | 88 | // output data |
ivo_david_michelle | 5:3ad830e95ffd | 89 | fprintf(fp, "# received\t= %d\n", successCounter); |
ivo_david_michelle | 5:3ad830e95ffd | 90 | fprintf(fp, "# sent\t= %d\n", iterations); |
ivo_david_michelle | 5:3ad830e95ffd | 91 | fprintf(fp, "success rate\t= %f\n", (double) successCounter / (double) iterations); |
ivo_david_michelle | 5:3ad830e95ffd | 92 | double sum = 0; |
ivo_david_michelle | 5:3ad830e95ffd | 93 | for (int i = 0; i < successCounter; i++) { |
ivo_david_michelle | 5:3ad830e95ffd | 94 | sum += rssiBuffer[i]; |
ivo_david_michelle | 5:3ad830e95ffd | 95 | } |
ivo_david_michelle | 5:3ad830e95ffd | 96 | fprintf(fp, "average rssi\t= %f\n", sum / (double) successCounter); |
ivo_david_michelle | 5:3ad830e95ffd | 97 | fclose(fp); |
ivo_david_michelle | 5:3ad830e95ffd | 98 | |
ivo_david_michelle | 5:3ad830e95ffd | 99 | pc.printf("END\r\n"); |
ivo_david_michelle | 5:3ad830e95ffd | 100 | } |