funziona

Dependencies:   SDFileSystem_motore mbed

Fork of SDFileSystem_HelloWorld by mbed official

Committer:
NdA994
Date:
Fri Feb 09 11:57:16 2018 +0000
Revision:
2:88534a51c2b4
Parent:
0:bdbd3d6fc5d5
Child:
3:4d9b3d817b27
funziona;

Who changed what in which revision?

UserRevisionLine numberNew 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
mbed_official 0:bdbd3d6fc5d5 11
NdA994 2:88534a51c2b4 12 FILE *fp;
NdA994 2:88534a51c2b4 13
NdA994 2:88534a51c2b4 14 int rpm;
NdA994 2:88534a51c2b4 15
NdA994 2:88534a51c2b4 16 typedef struct {
NdA994 2:88534a51c2b4 17 uint16_t ax;
NdA994 2:88534a51c2b4 18 uint16_t ay;
NdA994 2:88534a51c2b4 19 uint16_t az;
NdA994 2:88534a51c2b4 20 uint16_t gx;
NdA994 2:88534a51c2b4 21 uint16_t gy;
NdA994 2:88534a51c2b4 22 uint16_t gz;
NdA994 2:88534a51c2b4 23 } message_t;
NdA994 2:88534a51c2b4 24
NdA994 2:88534a51c2b4 25 void inviaMessaggio(int ax,int ay,int az,int gx,int gy,int gz){
NdA994 2:88534a51c2b4 26 message_t message;
NdA994 2:88534a51c2b4 27 message.ax = ax;
NdA994 2:88534a51c2b4 28 message.ay = ay;
NdA994 2:88534a51c2b4 29 message.az = az;
NdA994 2:88534a51c2b4 30 message.gx = gx;
NdA994 2:88534a51c2b4 31 message.gy = gy;
NdA994 2:88534a51c2b4 32 message.gz = gz;
NdA994 2:88534a51c2b4 33 fwrite(&message, sizeof(message_t), 1, fp);
NdA994 2:88534a51c2b4 34 }
mbed_official 0:bdbd3d6fc5d5 35
mbed_official 0:bdbd3d6fc5d5 36 int main() {
NdA994 2:88534a51c2b4 37 printf("Sono partito\n");
mbed_official 0:bdbd3d6fc5d5 38
mbed_official 0:bdbd3d6fc5d5 39 mkdir("/sd/mydir", 0777);
mbed_official 0:bdbd3d6fc5d5 40
NdA994 2:88534a51c2b4 41 fp = fopen("/sd/mydir/1.txt", "a");
mbed_official 0:bdbd3d6fc5d5 42 if(fp == NULL) {
mbed_official 0:bdbd3d6fc5d5 43 error("Could not open file for write\n");
mbed_official 0:bdbd3d6fc5d5 44 }
NdA994 2:88534a51c2b4 45 //fprintf(fp, "Hello fun SD Card World!");
NdA994 2:88534a51c2b4 46
NdA994 2:88534a51c2b4 47 unsigned int bb;
NdA994 2:88534a51c2b4 48 pc.baud(921600);
NdA994 2:88534a51c2b4 49 CANMessage msg;
NdA994 2:88534a51c2b4 50 //wait(15);
NdA994 2:88534a51c2b4 51 wait_ms(100);
NdA994 2:88534a51c2b4 52 pc.printf("\t CANBUS Attivo!! \r\n\n");
NdA994 2:88534a51c2b4 53
NdA994 2:88534a51c2b4 54 can.frequency(500000);
NdA994 2:88534a51c2b4 55 fprintf(fp, "NN\n\r");
NdA994 2:88534a51c2b4 56 while(1){
NdA994 2:88534a51c2b4 57 wait_ms(50);
NdA994 2:88534a51c2b4 58 //(inviaMessaggio(600, 600, 600, 6500, 6500, 6500);
NdA994 2:88534a51c2b4 59 /*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 60 As the data we are interested in is subdivided into various different address packets.
NdA994 2:88534a51c2b4 61 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 62 for(int i=0;i<50;i++){
NdA994 2:88534a51c2b4 63 wait_us(1);
NdA994 2:88534a51c2b4 64 if(can.read(msg)) {
NdA994 2:88534a51c2b4 65 //led1= !led1;
NdA994 2:88534a51c2b4 66 //printf("%d\r\n",msg.id);
NdA994 2:88534a51c2b4 67 switch (msg.id){
NdA994 2:88534a51c2b4 68 case(0x1f0a000):
NdA994 2:88534a51c2b4 69 unsigned int rpm_msb=msg.data[0]; //primo byte dei giri motore
NdA994 2:88534a51c2b4 70 unsigned int rpm_lsb=msg.data[1]; //secondo byte dei giri motore
NdA994 2:88534a51c2b4 71 // 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 72
NdA994 2:88534a51c2b4 73 /*rpm=rpm_msb*256;
NdA994 2:88534a51c2b4 74 rpm=rpm+rpm_lsb;
NdA994 2:88534a51c2b4 75 rpm=rpm*rpm_scale;*/
NdA994 2:88534a51c2b4 76 fprintf(fp, "%d %d\n\r", rpm_msb, rpm_lsb);
NdA994 2:88534a51c2b4 77 pc.printf("rpm= %d %d\n\r",rpm_msb, rpm_lsb);
NdA994 2:88534a51c2b4 78 break;
NdA994 2:88534a51c2b4 79 case(0x1f0a004):
NdA994 2:88534a51c2b4 80 // pc.printf("P_olio= %f\r\n",msg.data[4]*P_olio_scale);
NdA994 2:88534a51c2b4 81 printf("\tolio= %f \n\r",msg.data[4]*P_olio_scale);
NdA994 2:88534a51c2b4 82
NdA994 2:88534a51c2b4 83 }
NdA994 2:88534a51c2b4 84
NdA994 2:88534a51c2b4 85 }
NdA994 2:88534a51c2b4 86 if((can.rderror()+can.tderror()))pc.printf("Errori! : rd: %d\t td: %d\r\n",can.rderror(),can.tderror());
NdA994 2:88534a51c2b4 87 wait_us(10);
NdA994 2:88534a51c2b4 88 }
NdA994 2:88534a51c2b4 89 }
NdA994 2:88534a51c2b4 90 //fclose(fp);
mbed_official 0:bdbd3d6fc5d5 91
NdA994 2:88534a51c2b4 92 //printf("Goodbye World!\n");
mbed_official 0:bdbd3d6fc5d5 93 }