The project is not done yet
Dependencies: USBHost USBHostXpad mbed-rtos mbed
Fork of Totaleprogramma by
ultrasoon.h@1:da390b3b1330, 2015-03-02 (annotated)
- Committer:
- juliandekker
- Date:
- Mon Mar 02 10:58:30 2015 +0000
- Revision:
- 1:da390b3b1330
- Parent:
- 0:345f76c72b9a
xbox controller robot
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
347467 | 0:345f76c72b9a | 1 | //#include "mbed.h" |
347467 | 0:345f76c72b9a | 2 | |
347467 | 0:345f76c72b9a | 3 | I2C i2cMod(p28, p27); |
347467 | 0:345f76c72b9a | 4 | //Serial PC(USBTX, USBRX); //Debug port to PC |
347467 | 0:345f76c72b9a | 5 | |
347467 | 0:345f76c72b9a | 6 | int i2cAddress1 = 0xF2; |
347467 | 0:345f76c72b9a | 7 | int i2cAddress2 = 0xE0; |
347467 | 0:345f76c72b9a | 8 | |
347467 | 0:345f76c72b9a | 9 | void sendStartRangingCommand1(void){ |
347467 | 0:345f76c72b9a | 10 | const char command[] = {0x00, 0x51}; |
347467 | 0:345f76c72b9a | 11 | i2cMod.write(i2cAddress1, command, 2); |
347467 | 0:345f76c72b9a | 12 | } |
347467 | 0:345f76c72b9a | 13 | void sendStartRangingCommand2(void){ |
347467 | 0:345f76c72b9a | 14 | const char command[] = {0x00, 0x51}; |
347467 | 0:345f76c72b9a | 15 | i2cMod.write(i2cAddress2, command, 2); |
347467 | 0:345f76c72b9a | 16 | } |
347467 | 0:345f76c72b9a | 17 | /* |
347467 | 0:345f76c72b9a | 18 | int readRange1(void){ |
347467 | 0:345f76c72b9a | 19 | const char command[] = {0x02}; //Address of range register |
347467 | 0:345f76c72b9a | 20 | char response[] = {0x00, 0x00}; |
347467 | 0:345f76c72b9a | 21 | i2cMod.write(i2cAddress1, command, 1, 1); //Send command |
347467 | 0:345f76c72b9a | 22 | i2cMod.read(i2cAddress1, response, 2); //Read 16bits result |
347467 | 0:345f76c72b9a | 23 | int range = (response[0]<<8)+response[1]; //Shift two bytes into int |
347467 | 0:345f76c72b9a | 24 | return range; |
347467 | 0:345f76c72b9a | 25 | } |
347467 | 0:345f76c72b9a | 26 | |
347467 | 0:345f76c72b9a | 27 | int readRange2(void){ |
347467 | 0:345f76c72b9a | 28 | const char command[] = {0x02}; //Address of range register |
347467 | 0:345f76c72b9a | 29 | char response[] = {0x00, 0x00}; |
347467 | 0:345f76c72b9a | 30 | i2cMod.write(i2cAddress2, command, 1, 1); //Send command |
347467 | 0:345f76c72b9a | 31 | i2cMod.read(i2cAddress2, response, 2); //Read 16bits result |
347467 | 0:345f76c72b9a | 32 | int range = (response[0]<<8)+response[1]; //Shift two bytes into int |
347467 | 0:345f76c72b9a | 33 | return range; |
347467 | 0:345f76c72b9a | 34 | } |
347467 | 0:345f76c72b9a | 35 | |
347467 | 0:345f76c72b9a | 36 | //void setAddress(int address) { |
347467 | 0:345f76c72b9a | 37 | //Send address change sequence |
347467 | 0:345f76c72b9a | 38 | // char command[] = {0x00, 0xA0}; |
347467 | 0:345f76c72b9a | 39 | // i2cMod.write(i2cAddress, command, 2); |
347467 | 0:345f76c72b9a | 40 | // command[1] = 0xAA; |
347467 | 0:345f76c72b9a | 41 | // i2cMod.write(i2cAddress, command, 2); |
347467 | 0:345f76c72b9a | 42 | // command[1] = 0xA5; |
347467 | 0:345f76c72b9a | 43 | // i2cMod.write(i2cAddress, command, 2); |
347467 | 0:345f76c72b9a | 44 | // command[1] = address; |
347467 | 0:345f76c72b9a | 45 | // i2cMod.write(i2cAddress, command, 2); |
347467 | 0:345f76c72b9a | 46 | // } |
347467 | 0:345f76c72b9a | 47 | |
347467 | 0:345f76c72b9a | 48 | //int main() { |
347467 | 0:345f76c72b9a | 49 | // //Read software version |
347467 | 0:345f76c72b9a | 50 | //const char command[] = {0x00}; //Address of swversion register |
347467 | 0:345f76c72b9a | 51 | //char response[] = {0x00, 0x00}; |
347467 | 0:345f76c72b9a | 52 | //i2cMod.write(i2cAddress1, command, 1, 1); //Send command |
347467 | 0:345f76c72b9a | 53 | //i2cMod.read(i2cAddress1, response, 2); //Read 16bits result |
347467 | 0:345f76c72b9a | 54 | //int swversion = (response[0]<<8)+response[1]; //Shift two bytes into int |
347467 | 0:345f76c72b9a | 55 | //PC.printf(" Software version1: %i", swversion); |
347467 | 0:345f76c72b9a | 56 | //PC.printf("\n\r"); |
347467 | 0:345f76c72b9a | 57 | |
347467 | 0:345f76c72b9a | 58 | //Read software version |
347467 | 0:345f76c72b9a | 59 | //const char command[] = {0x00}; //Address of swversion register |
347467 | 0:345f76c72b9a | 60 | // char response[] = {0x00, 0x00}; |
347467 | 0:345f76c72b9a | 61 | // i2cMod.write(i2cAddress2, command, 1, 1); //Send command |
347467 | 0:345f76c72b9a | 62 | // i2cMod.read(i2cAddress2, response, 2); //Read 16bits result |
347467 | 0:345f76c72b9a | 63 | // int swversion = (response[0]<<8)+response[1]; //Shift two bytes into int |
347467 | 0:345f76c72b9a | 64 | // PC.printf(" Software version2: %i", swversion); |
347467 | 0:345f76c72b9a | 65 | // PC.printf("\n\r"); |
347467 | 0:345f76c72b9a | 66 | |
347467 | 0:345f76c72b9a | 67 | |
347467 | 0:345f76c72b9a | 68 | //setAddress(0xF2) ; |
347467 | 0:345f76c72b9a | 69 | |
347467 | 0:345f76c72b9a | 70 | //while(true){ |
347467 | 0:345f76c72b9a | 71 | // sendStartRangingCommand1(); |
347467 | 0:345f76c72b9a | 72 | // wait(0.07); |
347467 | 0:345f76c72b9a | 73 | // int range1 = readRange1(); |
347467 | 0:345f76c72b9a | 74 | // PC.printf(" Range1: %i", range1); |
347467 | 0:345f76c72b9a | 75 | // PC.printf("\n\r"); |
347467 | 0:345f76c72b9a | 76 | // wait (0.2); |
347467 | 0:345f76c72b9a | 77 | |
347467 | 0:345f76c72b9a | 78 | // sendStartRangingCommand2(); |
347467 | 0:345f76c72b9a | 79 | // wait(0.07); |
347467 | 0:345f76c72b9a | 80 | // int range2 = readRange2(); |
347467 | 0:345f76c72b9a | 81 | // PC.printf(" Range2: %i", range2); |
347467 | 0:345f76c72b9a | 82 | // PC.printf("\n\r"); |
347467 | 0:345f76c72b9a | 83 | // wait(1.0); |
347467 | 0:345f76c72b9a | 84 | //} |
347467 | 0:345f76c72b9a | 85 | //} |