Complete library for whole scientific pre-final

Dependencies:   BOX_1

Files at this revision

API Documentation at this revision

Comitter:
Alessio_Zaino
Date:
Thu Sep 05 20:56:11 2019 +0000
Parent:
4:f693e434d21c
Commit message:
final

Changed in this revision

BOX.lib Show annotated file Show diff for this revision Revisions of this file
ENVIROMENTAL/ENVIROMENTAL.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- a/BOX.lib	Tue Jun 11 12:57:01 2019 +0000
+++ b/BOX.lib	Thu Sep 05 20:56:11 2019 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/teams/Team-DIANA/code/BOX_1/#442e7d2ab496
+https://os.mbed.com/teams/Team-DIANA/code/BOX_1/#d9c5d93963a8
--- a/ENVIROMENTAL/ENVIROMENTAL.cpp	Tue Jun 11 12:57:01 2019 +0000
+++ b/ENVIROMENTAL/ENVIROMENTAL.cpp	Thu Sep 05 20:56:11 2019 +0000
@@ -42,9 +42,9 @@
     float VRL;
     float Rs;
     float ratio;
-    float Ro;
+    float Ro=28;
     float ppm;
-    float RL;
+    float RL=10;
     float m =-0.661;
     float b =1.332;
     for(int test_cycle = 1 ; test_cycle <= 500 ; test_cycle++) //Read the analog output of the sensor for 200 times
--- a/main.cpp	Tue Jun 11 12:57:01 2019 +0000
+++ b/main.cpp	Thu Sep 05 20:56:11 2019 +0000
@@ -1,60 +1,254 @@
+//SCIENTIFIC CAN ID'S 201 TO 255
+
+/***********
+ Open box1  ||201|0|| can frame id 
+ 201 indirizzamento al sottosistema (scatole)
+ 202 indirizzamento dal sottosistema (scatole)
+ 203/4 indirizzamento dal sottosistema (envi)
+ 0  id scatola 1
+ 
+ nel dato
+ 100 chiudi
+ 101 apri
+ 102 leggi
+
+
+ CAN RX priorità piu bassa
+ envi priorità normale
+ box alta
+
+
+
+
+Costruzione frame CAN
+ void sci::buildFrame(struct can_frame &frame, uint8_t id, can_commands command, int32_t value)
+{
+ int len = 0;
+ uint32_t new_id = id;
+ new_id |= (uint32_t)command << 8;
+ new_id |= 0x80000000;  100|0 0000 0000 0000 0  000 0000| 0000 0000
+ frame.can_id = new_id;
+
+ frame.data[len++] = value >> 24;
+ frame.data[len++] = value >> 16;
+ frame.data[len++] = value >> 8;
+ frame.data[len++] = value;
+
+ frame.can_dlc = len;
+}
+************/
 #include "BOX.h"
+#include "mbed.h"
 #include "ENVIROMENTAL.h"
-DigitalOut led(LED3);
-I2C i2c(D14,D15);
-BOX box1 (D14,D15,D8,A0,5,D3);
-BOX box2 (D14,D15,D6,A0,5,D3);
-BOX box3 (D14,D15,D4,A0,5,D3);
-BOX box4 (D14,D15,D2,A0,5,D3);
-ENVIROMENTAL envi(D14, D15,A1,i2c);
-int main(){
-  //BOX *m_box = new BOX(D1,D2,D3,A0,5);
-box1.initialize();
-  box2.initialize();
-  box3.initialize();
-  box4.initialize();
-  while(1) {
-        int i,j,k;
- float t;
- int a;
-  t=box1.get_temp();    //return temperature from DS18B20
- a=box1.get_resistance();//return soil resistance
+
+//EVENT FLAGS
+#define OPEN_BOX  101
+#define CLOSE_BOX 100
+#define SEND_BOX_DATA_REQUEST 102
+
+Thread can_rx_thd;
+Thread box_thd;
+Thread envi_thd;
+
+EventFlags event_flags;
+
+CAN can(PB_8, PB_9);
+CANMessage msg;
+CANFormat CANextended;
 
- //t=m_box.get_resistance();
-  printf("temp %f       res  %d\n\r",t,a);
-  wait(1);
-    t=box2.get_temp();    //return temperature from DS18B20
- a=box2.get_resistance();//return soil resistance
+BOX box1(PA_9, PA_8, PB_0, PA_0, 1000, PA_4);
+BOX box2(PC_9, PC_8, PB_1, PA_1, 1000, PA_5);
+BOX box3(PC_7, PC_6, PB_2, PA_2, 1000, PA_6);
+I2C i2c(PB_7,PB_6);
+ENVIROMENTAL envi(PB_7, PB_6, PA_3, i2c);
+
+unsigned char box_sel;
+unsigned char box_cmd;
+
+char message_out[8];
+char envi_tx[8];
+char data_box[3][8];
+
+uint32_t new_id(int command, int id)
+{
+    uint32_t id_message = id;
+    id_message |= (uint32_t)command << 8;
+    id_message |= 0x80000000;
 
-  printf("temp %f       res  %d\n\r",t,a);
-  wait(1);
-  
-  t=box3.get_temp();    //return temperature from DS18B20
- a=box3.get_resistance();//return soil resistance
+    return id_message;
+}
+void box()
+{
+    uint32_t flag_read = 0; 
+    box1.initialize();
+    box2.initialize(); 
+    box3.initialize();
+    int weight = 0;
+    int resistence = 0;
+    float temperature = 0;
+    while(1)
+    {
+        flag_read = event_flags.wait_any(OPEN_BOX | CLOSE_BOX | SEND_BOX_DATA_REQUEST); //trigger this theand only if
+        switch (flag_read)
+        {
+            case OPEN_BOX:
+                switch(box_sel)
+                {
+                    case 1:
+                        box1.move_servo(1000);
+                        //open box 1
+                        break;
+                    case 2:
+                        box2.move_servo(1000);
+                        //open box 2
+                        break;
+                    case 3:
+                        box3.move_servo(1000);
+                        //open box 3
+                        break;
+                    default: break;
+                }
+                break;
+            case CLOSE_BOX:
+                switch(box_sel)
+                {
+                    case 1:
+                        box2.move_servo(2200);
+                        //close box 1
+                        break;
+                    case 2:
+                        box2.move_servo(2200);
+                        //close box 2
+                        break;
+                    case 3:
+                        box2.move_servo(2200);
+                        //close box 3
+                        break;
+                    default: break;
+                }
+                break;
 
-  printf("temp %f       res  %d\n\r",t,a);
-  wait(1);
-  
-  t=box4.get_temp();    //return temperature from DS18B20
- a=box4.get_resistance();//return soil resistance
+            case SEND_BOX_DATA_REQUEST:
+            //for every data request, a measure is made and then the data is sended on CAN
+                switch(box_sel)
+                {
+                    case 1:
+                        //make measurements box 1
+                        weight = box1.get_weight();
+                        resistence = box1.get_resistance();  //bisogna ancora "impacchettare i dati"
+                        temperature = box1.get_temp();
+                        //temperature 4 bytes
+                        *(float*)(data_box[0]) = temperature;
+                        //weight 2 bytes 
+                        data_box[0][4] = (char)(weight >> 8);
+                        data_box[0][5] = (char)(weight);
+                        //resistence 2 bytes
+                        data_box[0][6] = (char)(resistence >> 8);
+                        data_box[0][7] = (char)(resistence);
+                        break;
+                    case 2:
+                        //make measurements box 2
+                        weight = box2.get_weight();
+                        resistence = box2.get_resistance();
+                        temperature = box2.get_temp();
+                        //temperature 4 bytes
+                        *(float*)(data_box[1]) = temperature;
+                        //weight 2 bytes 
+                        data_box[1][4] = (char)(weight >> 8);
+                        data_box[1][5] = (char)(weight);
+                        //resistence 2 bytes
+                        data_box[1][6] = (char)(resistence >> 8);
+                        data_box[1][7] = (char)(resistence);
+                        break;
+                    case 3:
+                        //make measurements box 3
+                        weight = box3.get_weight();
+                        resistence = box3.get_resistance();
+                        temperature = box3.get_temp();
+                        //temperature 4 bytes
+                        *(float*)(data_box[2]) = temperature;
+                        //weight 2 bytes 
+                        data_box[2][4] = (char)(weight >> 8);
+                        data_box[2][5] = (char)(weight);
+                        //resistence 2 bytes
+                        data_box[2][6] = (char)(resistence >> 8);
+                        data_box[2][7] = (char)(resistence);
+                        break;
+                    default: break;
+                }
+                for(int i=0; i<8; i++)
+                {
+                    message_out[i] = data_box[box_sel][i];
+                }
+                can.write(CANMessage(new_id(202, box_sel),message_out, 8));
+                break;
+            default: break;
+        }
+    }
+}
 
-  printf("temp %f       res  %d\n\r",t,a);
-  wait(1);
-  
+void can_rx()
+{
+    while(1)
+    {
+        can.read(msg);
+
+        switch (msg.id)
+        {
+            case 201:
+                box_sel = msg.data[0];
+                box_cmd = msg.data[1];
+                event_flags.set(OPEN_BOX);
+                break;
+            case 202:
+                box_sel = msg.data[0];
+                box_cmd = msg.data[1];
+                event_flags.set(CLOSE_BOX);
+                break;
+            case 203:
+                box_sel = msg.data[0];
+                event_flags.set(SEND_BOX_DATA_REQUEST);
+                break;
+
+        }
+    }
+}
+
+void environmental() //make measurements and send to the can bus every second
+{     
+    float temp,CO;
+    uint32_t hum;
+    int32_t pres;
+    int lumen, infrared, uv;
+
+   temp = envi.get_temp();    //return temperature from SI7021
+  hum = envi.get_humidity(); //return humidity from SI7021
+  pres =  envi.get_pressure();    //return pressure in Pa from MBP280
+  CO = envi.get_CO();  //return CO from  mq-7
+  lumen =  envi.get_lumen();   //return visible lumen from TSL2561
+  infrared = envi.get_infrared();   //return infrared lumen from TSL2561
+  uv = envi.get_uv();    //return uv intensity from VEML6070
   
-  
-  
- /* 
-  
-  
+  envi_tx[1]=(char)(temp);
+  envi_tx[2]=(char)(hum);
+  envi_tx[3]=(char)(pres);
+  envi_tx[4]=(char)(CO);
+  envi_tx[5]=(char)(lumen);
+  envi_tx[6]=(char)(infrared);
+  envi_tx[7]=(char)(uv);  
   
-  t=envi.get_temp();    //return temperature from SI7021
-  i=envi.get_humidity(); //return humidity from SI7021
-  j=envi.get_pressure();    //return pressure in Pa from MBP280
-  a=envi.get_CO();  //return CO from  mq-7
-  k=envi.get_lux();   //return lux from TSL2561
-  printf("temp %f       hum  %d    pres  %d      co %f    lux %d\n\r",t,i,j,a,k);
-  
-  }
-   */
-}}
\ No newline at end of file
+    
+    can.write(CANMessage(210, envi_tx, 8));
+        osDelay(1000);
+}
+
+
+int main()
+{ 
+    can.frequency(125000);
+    box_thd.start(mbed::callback(box));
+    envi_thd.start(environmental);
+    can_rx_thd.start(can_rx);
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Thu Sep 05 20:56:11 2019 +0000
@@ -0,0 +1,8 @@
+{
+ "target_overrides": {
+       "NUCLEO_F446RE": {
+               "platform.stdio-baud-rate": 115200,
+              "target.clock_source": "USE_PLL_HSE_XTAL"
+              }
+       }
+}
\ No newline at end of file