funziona
Dependencies: SDFileSystem_motore mbed
Fork of SDFileSystem_HelloWorld by
main.cpp@3:4d9b3d817b27, 2018-02-09 (annotated)
- Committer:
- NdA994
- Date:
- Fri Feb 09 15:51:34 2018 +0000
- Revision:
- 3:4d9b3d817b27
- Parent:
- 2:88534a51c2b4
funziona;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:bdbd3d6fc5d5 | 1 | #include "mbed.h" |
mbed_official | 0:bdbd3d6fc5d5 | 2 | #include "SDFileSystem.h" |
NdA994 | 2:88534a51c2b4 | 3 | |
NdA994 | 2:88534a51c2b4 | 4 | const float rpm_scale=0.39063; //scala di conversione |
NdA994 | 2:88534a51c2b4 | 5 | const float P_olio_scale=0.580151*0.0689476; |
NdA994 | 2:88534a51c2b4 | 6 | |
NdA994 | 2:88534a51c2b4 | 7 | CAN can(D10,D2); //Rx & TX da mettere con PB_12, PB_13 |
NdA994 | 2:88534a51c2b4 | 8 | Serial pc(USBTX, USBRX); // tx, rx |
NdA994 | 2:88534a51c2b4 | 9 | |
NdA994 | 2:88534a51c2b4 | 10 | SDFileSystem sd(A6, A5, A4, A3, "sd"); // the pinout on the mbed Cool Components workshop board |
NdA994 | 3:4d9b3d817b27 | 11 | int n = 0; |
mbed_official | 0:bdbd3d6fc5d5 | 12 | |
NdA994 | 2:88534a51c2b4 | 13 | FILE *fp; |
NdA994 | 2:88534a51c2b4 | 14 | |
NdA994 | 3:4d9b3d817b27 | 15 | DigitalIn mybutton(D1); |
NdA994 | 3:4d9b3d817b27 | 16 | |
NdA994 | 2:88534a51c2b4 | 17 | int rpm; |
NdA994 | 2:88534a51c2b4 | 18 | |
NdA994 | 2:88534a51c2b4 | 19 | typedef struct { |
NdA994 | 2:88534a51c2b4 | 20 | uint16_t ax; |
NdA994 | 2:88534a51c2b4 | 21 | uint16_t ay; |
NdA994 | 2:88534a51c2b4 | 22 | uint16_t az; |
NdA994 | 2:88534a51c2b4 | 23 | uint16_t gx; |
NdA994 | 2:88534a51c2b4 | 24 | uint16_t gy; |
NdA994 | 2:88534a51c2b4 | 25 | uint16_t gz; |
NdA994 | 2:88534a51c2b4 | 26 | } message_t; |
NdA994 | 2:88534a51c2b4 | 27 | |
NdA994 | 2:88534a51c2b4 | 28 | void inviaMessaggio(int ax,int ay,int az,int gx,int gy,int gz){ |
NdA994 | 2:88534a51c2b4 | 29 | message_t message; |
NdA994 | 2:88534a51c2b4 | 30 | message.ax = ax; |
NdA994 | 2:88534a51c2b4 | 31 | message.ay = ay; |
NdA994 | 2:88534a51c2b4 | 32 | message.az = az; |
NdA994 | 2:88534a51c2b4 | 33 | message.gx = gx; |
NdA994 | 2:88534a51c2b4 | 34 | message.gy = gy; |
NdA994 | 2:88534a51c2b4 | 35 | message.gz = gz; |
NdA994 | 2:88534a51c2b4 | 36 | fwrite(&message, sizeof(message_t), 1, fp); |
NdA994 | 2:88534a51c2b4 | 37 | } |
mbed_official | 0:bdbd3d6fc5d5 | 38 | |
mbed_official | 0:bdbd3d6fc5d5 | 39 | int main() { |
NdA994 | 2:88534a51c2b4 | 40 | printf("Sono partito\n"); |
mbed_official | 0:bdbd3d6fc5d5 | 41 | |
mbed_official | 0:bdbd3d6fc5d5 | 42 | mkdir("/sd/mydir", 0777); |
mbed_official | 0:bdbd3d6fc5d5 | 43 | |
NdA994 | 2:88534a51c2b4 | 44 | fp = fopen("/sd/mydir/1.txt", "a"); |
mbed_official | 0:bdbd3d6fc5d5 | 45 | if(fp == NULL) { |
mbed_official | 0:bdbd3d6fc5d5 | 46 | error("Could not open file for write\n"); |
mbed_official | 0:bdbd3d6fc5d5 | 47 | } |
NdA994 | 2:88534a51c2b4 | 48 | //fprintf(fp, "Hello fun SD Card World!"); |
NdA994 | 2:88534a51c2b4 | 49 | |
NdA994 | 2:88534a51c2b4 | 50 | unsigned int bb; |
NdA994 | 2:88534a51c2b4 | 51 | pc.baud(921600); |
NdA994 | 2:88534a51c2b4 | 52 | CANMessage msg; |
NdA994 | 2:88534a51c2b4 | 53 | //wait(15); |
NdA994 | 2:88534a51c2b4 | 54 | wait_ms(100); |
NdA994 | 2:88534a51c2b4 | 55 | pc.printf("\t CANBUS Attivo!! \r\n\n"); |
NdA994 | 2:88534a51c2b4 | 56 | |
NdA994 | 2:88534a51c2b4 | 57 | can.frequency(500000); |
NdA994 | 2:88534a51c2b4 | 58 | fprintf(fp, "NN\n\r"); |
NdA994 | 2:88534a51c2b4 | 59 | while(1){ |
NdA994 | 2:88534a51c2b4 | 60 | wait_ms(50); |
NdA994 | 2:88534a51c2b4 | 61 | //(inviaMessaggio(600, 600, 600, 6500, 6500, 6500); |
NdA994 | 2:88534a51c2b4 | 62 | /*The control unit sends all the data that it carries on the can-bus, they are subdivided into various packs with various labels, depending on the data extraction log. |
NdA994 | 2:88534a51c2b4 | 63 | As the data we are interested in is subdivided into various different address packets. |
NdA994 | 2:88534a51c2b4 | 64 | Since the unsupported but unsolicited mails take them all in this way in the end I'm sure the updated data will be true to the last 50 ms */ |
NdA994 | 2:88534a51c2b4 | 65 | for(int i=0;i<50;i++){ |
NdA994 | 2:88534a51c2b4 | 66 | wait_us(1); |
NdA994 | 2:88534a51c2b4 | 67 | if(can.read(msg)) { |
NdA994 | 2:88534a51c2b4 | 68 | //led1= !led1; |
NdA994 | 2:88534a51c2b4 | 69 | //printf("%d\r\n",msg.id); |
NdA994 | 2:88534a51c2b4 | 70 | switch (msg.id){ |
NdA994 | 2:88534a51c2b4 | 71 | case(0x1f0a000): |
NdA994 | 2:88534a51c2b4 | 72 | unsigned int rpm_msb=msg.data[0]; //primo byte dei giri motore |
NdA994 | 2:88534a51c2b4 | 73 | unsigned int rpm_lsb=msg.data[1]; //secondo byte dei giri motore |
NdA994 | 2:88534a51c2b4 | 74 | // int rpm= (rpm_msb+rpm_lsb<<8)*rpm_scale; //rpm complessivo tenendo conto del peso dei primi 8 bit e della scala di conversione; |
NdA994 | 2:88534a51c2b4 | 75 | |
NdA994 | 2:88534a51c2b4 | 76 | /*rpm=rpm_msb*256; |
NdA994 | 2:88534a51c2b4 | 77 | rpm=rpm+rpm_lsb; |
NdA994 | 2:88534a51c2b4 | 78 | rpm=rpm*rpm_scale;*/ |
NdA994 | 2:88534a51c2b4 | 79 | fprintf(fp, "%d %d\n\r", rpm_msb, rpm_lsb); |
NdA994 | 2:88534a51c2b4 | 80 | pc.printf("rpm= %d %d\n\r",rpm_msb, rpm_lsb); |
NdA994 | 2:88534a51c2b4 | 81 | break; |
NdA994 | 2:88534a51c2b4 | 82 | case(0x1f0a004): |
NdA994 | 2:88534a51c2b4 | 83 | // pc.printf("P_olio= %f\r\n",msg.data[4]*P_olio_scale); |
NdA994 | 2:88534a51c2b4 | 84 | printf("\tolio= %f \n\r",msg.data[4]*P_olio_scale); |
NdA994 | 2:88534a51c2b4 | 85 | |
NdA994 | 2:88534a51c2b4 | 86 | } |
NdA994 | 2:88534a51c2b4 | 87 | |
NdA994 | 2:88534a51c2b4 | 88 | } |
NdA994 | 2:88534a51c2b4 | 89 | if((can.rderror()+can.tderror()))pc.printf("Errori! : rd: %d\t td: %d\r\n",can.rderror(),can.tderror()); |
NdA994 | 2:88534a51c2b4 | 90 | wait_us(10); |
NdA994 | 2:88534a51c2b4 | 91 | } |
NdA994 | 3:4d9b3d817b27 | 92 | if (mybutton == 0) { |
NdA994 | 3:4d9b3d817b27 | 93 | //chiusuraFile(); |
NdA994 | 3:4d9b3d817b27 | 94 | //aperturaFile(); |
NdA994 | 3:4d9b3d817b27 | 95 | //fprintf(fp, "###########FLAG MARCATORE PRESSIONE PULSANTE############\r\n"); |
NdA994 | 3:4d9b3d817b27 | 96 | fprintf(fp, "NN %d\n\r", n); |
NdA994 | 3:4d9b3d817b27 | 97 | printf("flag\n\r"); |
NdA994 | 3:4d9b3d817b27 | 98 | n++; |
NdA994 | 3:4d9b3d817b27 | 99 | wait(1); |
NdA994 | 3:4d9b3d817b27 | 100 | } |
NdA994 | 2:88534a51c2b4 | 101 | } |
NdA994 | 2:88534a51c2b4 | 102 | //fclose(fp); |
mbed_official | 0:bdbd3d6fc5d5 | 103 | |
NdA994 | 2:88534a51c2b4 | 104 | //printf("Goodbye World!\n"); |
mbed_official | 0:bdbd3d6fc5d5 | 105 | } |