Hawk Eye
/
ROVReceiver
Receiver for rc ROV
main.cpp@0:a8a417d50cc7, 2018-08-28 (annotated)
- Committer:
- spin7ion
- Date:
- Tue Aug 28 16:57:47 2018 +0000
- Revision:
- 0:a8a417d50cc7
- Child:
- 1:e11cc38ef502
Initial commit with telem
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
spin7ion | 0:a8a417d50cc7 | 1 | #include "mbed.h" |
spin7ion | 0:a8a417d50cc7 | 2 | #include "nRF24L01P.h" |
spin7ion | 0:a8a417d50cc7 | 3 | |
spin7ion | 0:a8a417d50cc7 | 4 | #define CHANNELS_NUMBER 12 |
spin7ion | 0:a8a417d50cc7 | 5 | #define CMD_VALUE 65535 |
spin7ion | 0:a8a417d50cc7 | 6 | #define CMD_GET_STATUS 1 |
spin7ion | 0:a8a417d50cc7 | 7 | //DigitalOut myled(LED1); |
spin7ion | 0:a8a417d50cc7 | 8 | |
spin7ion | 0:a8a417d50cc7 | 9 | |
spin7ion | 0:a8a417d50cc7 | 10 | #define SERVO_MAX 2500 |
spin7ion | 0:a8a417d50cc7 | 11 | #define SERVO_MIN 800 |
spin7ion | 0:a8a417d50cc7 | 12 | #define SERVO_CENTER SERVO_MAX-SERVO_MIN |
spin7ion | 0:a8a417d50cc7 | 13 | |
spin7ion | 0:a8a417d50cc7 | 14 | Serial pc(USBTX, USBRX); // tx, rx |
spin7ion | 0:a8a417d50cc7 | 15 | |
spin7ion | 0:a8a417d50cc7 | 16 | nRF24L01P radio(D11, D12, D13, D10, D9, D8); // mosi, miso, sck, csn, ce, irq |
spin7ion | 0:a8a417d50cc7 | 17 | |
spin7ion | 0:a8a417d50cc7 | 18 | uint16_t channels[CHANNELS_NUMBER]; |
spin7ion | 0:a8a417d50cc7 | 19 | uint16_t buffer [CHANNELS_NUMBER]; |
spin7ion | 0:a8a417d50cc7 | 20 | |
spin7ion | 0:a8a417d50cc7 | 21 | //PwmOut v_servo(PTC2); |
spin7ion | 0:a8a417d50cc7 | 22 | |
spin7ion | 0:a8a417d50cc7 | 23 | void zeroBuffer(){ |
spin7ion | 0:a8a417d50cc7 | 24 | memset(buffer, 0, sizeof(buffer)); |
spin7ion | 0:a8a417d50cc7 | 25 | } |
spin7ion | 0:a8a417d50cc7 | 26 | |
spin7ion | 0:a8a417d50cc7 | 27 | int main() { |
spin7ion | 0:a8a417d50cc7 | 28 | //v_servo.period_us(20000); // servo requires a 20ms period |
spin7ion | 0:a8a417d50cc7 | 29 | //v_servo.pulsewidth_us(SERVO_CENTER); |
spin7ion | 0:a8a417d50cc7 | 30 | //v_servo.pulsewidth_us(pulse); // servo position determined by a pulsewidth between 1-2ms |
spin7ion | 0:a8a417d50cc7 | 31 | |
spin7ion | 0:a8a417d50cc7 | 32 | radio.powerUp(); |
spin7ion | 0:a8a417d50cc7 | 33 | pc.baud(115200); |
spin7ion | 0:a8a417d50cc7 | 34 | pc.printf("We all live in a yellow submarine!\r\n"); |
spin7ion | 0:a8a417d50cc7 | 35 | |
spin7ion | 0:a8a417d50cc7 | 36 | radio.setRxAddress(0xDEADBEEF0F); |
spin7ion | 0:a8a417d50cc7 | 37 | radio.setTxAddress(0xDEADC0DE0F); |
spin7ion | 0:a8a417d50cc7 | 38 | radio.setRfFrequency(2405); |
spin7ion | 0:a8a417d50cc7 | 39 | radio.powerUp(); |
spin7ion | 0:a8a417d50cc7 | 40 | |
spin7ion | 0:a8a417d50cc7 | 41 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", radio.getRfFrequency() ); |
spin7ion | 0:a8a417d50cc7 | 42 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", radio.getRfOutputPower() ); |
spin7ion | 0:a8a417d50cc7 | 43 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", radio.getAirDataRate() ); |
spin7ion | 0:a8a417d50cc7 | 44 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", radio.getTxAddress() ); |
spin7ion | 0:a8a417d50cc7 | 45 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", radio.getRxAddress() ); |
spin7ion | 0:a8a417d50cc7 | 46 | |
spin7ion | 0:a8a417d50cc7 | 47 | radio.setTransferSize( CHANNELS_NUMBER*sizeof(uint16_t) ); |
spin7ion | 0:a8a417d50cc7 | 48 | |
spin7ion | 0:a8a417d50cc7 | 49 | radio.setReceiveMode(); |
spin7ion | 0:a8a417d50cc7 | 50 | |
spin7ion | 0:a8a417d50cc7 | 51 | radio.enable(); |
spin7ion | 0:a8a417d50cc7 | 52 | |
spin7ion | 0:a8a417d50cc7 | 53 | pc.printf("Greetings, capitain!\r\n"); |
spin7ion | 0:a8a417d50cc7 | 54 | |
spin7ion | 0:a8a417d50cc7 | 55 | while (1) |
spin7ion | 0:a8a417d50cc7 | 56 | { |
spin7ion | 0:a8a417d50cc7 | 57 | while( !radio.readable() ); |
spin7ion | 0:a8a417d50cc7 | 58 | { |
spin7ion | 0:a8a417d50cc7 | 59 | int rxDataCnt = radio.read( NRF24L01P_PIPE_P0, (char*)buffer, sizeof( buffer ) ); |
spin7ion | 0:a8a417d50cc7 | 60 | |
spin7ion | 0:a8a417d50cc7 | 61 | if (buffer[0] == CMD_VALUE) { |
spin7ion | 0:a8a417d50cc7 | 62 | pc.printf("Command: %d\r\n", buffer[1]); |
spin7ion | 0:a8a417d50cc7 | 63 | switch(buffer[1]){ |
spin7ion | 0:a8a417d50cc7 | 64 | case CMD_GET_STATUS: |
spin7ion | 0:a8a417d50cc7 | 65 | zeroBuffer(); |
spin7ion | 0:a8a417d50cc7 | 66 | strcpy((char*)buffer,"I'm alive"); |
spin7ion | 0:a8a417d50cc7 | 67 | radio.setTransmitMode(); |
spin7ion | 0:a8a417d50cc7 | 68 | //wait(0.1); |
spin7ion | 0:a8a417d50cc7 | 69 | int bytesWritten = radio.write( NRF24L01P_PIPE_P0, (char*)buffer, sizeof( buffer ) ); |
spin7ion | 0:a8a417d50cc7 | 70 | if (bytesWritten<2*CHANNELS_NUMBER){ |
spin7ion | 0:a8a417d50cc7 | 71 | pc.printf("Transmit error\r\n"); |
spin7ion | 0:a8a417d50cc7 | 72 | } else { |
spin7ion | 0:a8a417d50cc7 | 73 | pc.printf("Sent telemetry: %s\r\n",(char*)buffer); |
spin7ion | 0:a8a417d50cc7 | 74 | } |
spin7ion | 0:a8a417d50cc7 | 75 | radio.setReceiveMode(); |
spin7ion | 0:a8a417d50cc7 | 76 | //wait(0.1); |
spin7ion | 0:a8a417d50cc7 | 77 | break; |
spin7ion | 0:a8a417d50cc7 | 78 | } |
spin7ion | 0:a8a417d50cc7 | 79 | } else { |
spin7ion | 0:a8a417d50cc7 | 80 | for(int i=0; i<CHANNELS_NUMBER; i++){ |
spin7ion | 0:a8a417d50cc7 | 81 | channels[i] = buffer[i]; |
spin7ion | 0:a8a417d50cc7 | 82 | } |
spin7ion | 0:a8a417d50cc7 | 83 | pc.printf("received channels: %d %d %d %d\n\r", channels[0],channels[1],channels[2],channels[3]); |
spin7ion | 0:a8a417d50cc7 | 84 | } |
spin7ion | 0:a8a417d50cc7 | 85 | } |
spin7ion | 0:a8a417d50cc7 | 86 | } |
spin7ion | 0:a8a417d50cc7 | 87 | |
spin7ion | 0:a8a417d50cc7 | 88 | } |