Commit 5c3e7a3caf45edee19c0618b0c7ca805afc95189

Authored by Eleazar Pérez Arencibia
1 parent 95889cb2
Exists in master

Parte del modo dormido terminado. El trabajo ha concluido!!!

bus_can1/bus_can1.ino
... ... @@ -11,7 +11,6 @@ int year;int month;int day;int hour;int minute;int second;
11 11 String aux;String aux2;String aux3;
12 12 uint32_t id;uint32_t mask;uint32_t filter;
13 13 uint32_t id_fr; uint32_t id_to;
14   -bool sending=false;
15 14  
16 15 /***Prototipo de las funciones***/
17 16 //Funcion para saber si el comando es valido
... ... @@ -66,7 +65,7 @@ boolean valid_command(String& str1,String& str2){
66 65 return true;
67 66 }else{
68 67 Serial.print("[ERROR] bad command \""); Serial.print(str1); Serial.println("\"");
69   - Serial.print("Commands accepted: "); Serial.println("\"send <message_with_maximum_8_characters> <recipient>\"");Serial.println("\"receive\"");Serial.println("\"stime <dd/mm/aa hh:mm:ss>\"");Serial.println("\"gtime\"");
  68 + Serial.print("Commands accepted: "); Serial.println("\"send <message_with_maximum_8_characters> <recipient:1,2_or_3>\"");Serial.println("\"receive\"");Serial.println("\"stime <dd/mm/aa hh:mm:ss>\"");Serial.println("\"gtime\"");
70 69 return false;
71 70 }
72 71 }
... ... @@ -77,14 +76,13 @@ void include_recipient(String str3){
77 76 void slct_command(String& str1,String& str2){
78 77  
79 78 if(str1=="help"){
80   - Serial.println("Commands accepted: "); Serial.println("\"send <message_with_maximum_8_characters> <recipient>\"");Serial.println("\"receive\"");Serial.println("\"stime <dd/mm/aa hh:mm:ss>\"");Serial.println("\"gtime\"");
  79 + Serial.println("Commands accepted: "); Serial.println("\"send <message_with_maximum_8_characters> <recipient:1,2_or_3>\"");Serial.println("\"receive\"");Serial.println("\"stime <dd/mm/aa hh:mm:ss>\"");Serial.println("\"gtime\"");
81 80 Serial.println("For sending messages: ");
82 81 Serial.println(" 1.- Send a message without data in these format: send <recipient>");
83   - Serial.println(" 2.- Send a massage with data in these format: send <message_with_maximum_8_characters> <recipient>");
  82 + Serial.println(" 2.- Send a massage with data in these format: send <message_with_maximum_8_characters> <recipient:1,2_or_3>");
84 83 }
85 84 if(str1=="send"){
86 85 include_recipient(string4);
87   - if(sending){
88 86 int count=0;
89 87 //Primer caracter de str2 es un espacio por el trim de antes
90 88 if(str2.length()<=8){
... ... @@ -114,21 +112,33 @@ void slct_command(String&amp; str1,String&amp; str2){
114 112 }else{
115 113 Serial.println("Error Buffer Full");
116 114 }
  115 + Serial.println("Waiting for acknowledgement...");
  116 + while(arcan.get_message(&message_rec) == 0);
  117 + Serial.println("Acknowledgement received");
117 118 arcan.mode_sleep();
118   - }else{
119   - if(arcan.check_free_buffer()){
120   - num_buffer=arcan.send_message(&message_sent);
121   - Serial.println("Now, send the message with data");
122   - sending=true;
123   - }else{
124   - Serial.println("Error Buffer Full");
125   - }
126   - }
127 119 }
128 120 if(str1=="receive"){
129 121 if(arcan.get_message(&message_rec) != 0){
130 122 arcan.print_message(&message_rec);
131 123 Serial.print("Message received");
  124 +
  125 + //Enviamos mensaje de confirmacion de recepcion
  126 + uint32_t id_fr=(message_rec.id>>5)&0x1F;
  127 + uint32_t id_to=(message_rec.id&0x1F);
  128 + message_sent.id=(id_to<<5)|(0x1F&id_fr);
  129 + message_sent.header.rtr=0;
  130 + message_sent.header.length=0;
  131 +
  132 + if(arcan.check_free_buffer()){
  133 + num_buffer=arcan.send_message(&message_sent);
  134 +
  135 + //Vaciamos buffer de entrada
  136 + while(arcan.get_message(&message_rec)!=0);
  137 +
  138 + if(num_buffer==0) Serial.println("Error sending");
  139 + }else{
  140 + Serial.println("Error Buffer Full");
  141 + }
132 142 }else{
133 143 Serial.print("There are no messages in buffer");
134 144 }
... ...
bus_can2/bus_can2.ino
... ... @@ -11,8 +11,6 @@ int year;int month;int day;int hour;int minute;int second;
11 11 String aux;String aux2;String aux3;
12 12 uint32_t id;uint32_t mask;uint32_t filter;
13 13 uint32_t id_fr; uint32_t id_to;
14   -bool sending=false;
15   -
16 14 /***Prototipo de las funciones***/
17 15 //Funcion para saber si el comando es valido
18 16 boolean valid_command(String& str1,String& str2);
... ... @@ -66,7 +64,7 @@ boolean valid_command(String&amp; str1,String&amp; str2){
66 64 return true;
67 65 }else{
68 66 Serial.print("[ERROR] bad command \""); Serial.print(str1); Serial.println("\"");
69   - Serial.print("Commands accepted: "); Serial.println("\"send <message_with_maximum_8_characters> <recipient>\"");Serial.println("\"receive\"");Serial.println("\"stime <dd/mm/aa hh:mm:ss>\"");Serial.println("\"gtime\"");
  67 + Serial.print("Commands accepted: "); Serial.println("\"send <message_with_maximum_8_characters> <recipient:1,2_or_3>\"");Serial.println("\"receive\"");Serial.println("\"stime <dd/mm/aa hh:mm:ss>\"");Serial.println("\"gtime\"");
70 68 return false;
71 69 }
72 70 }
... ... @@ -77,14 +75,13 @@ void include_recipient(String str3){
77 75 void slct_command(String& str1,String& str2){
78 76  
79 77 if(str1=="help"){
80   - Serial.println("Commands accepted: "); Serial.println("\"send <message_with_maximum_8_characters> <recipient>\"");Serial.println("\"receive\"");Serial.println("\"stime <dd/mm/aa hh:mm:ss>\"");Serial.println("\"gtime\"");
  78 + Serial.println("Commands accepted: "); Serial.println("\"send <message_with_maximum_8_characters> <recipient:1,2_or_3>\"");Serial.println("\"receive\"");Serial.println("\"stime <dd/mm/aa hh:mm:ss>\"");Serial.println("\"gtime\"");
81 79 Serial.println("For sending messages: ");
82 80 Serial.println(" 1.- Send a message without data in these format: send <recipient>");
83   - Serial.println(" 2.- Send a massage with data in these format: send <message_with_maximum_8_characters> <recipient>");
  81 + Serial.println(" 2.- Send a massage with data in these format: send <message_with_maximum_8_characters> <recipient:1,2_or_3>");
84 82 }
85 83 if(str1=="send"){
86 84 include_recipient(string4);
87   - if(sending){
88 85 int count=0;
89 86 //Primer caracter de str2 es un espacio por el trim de antes
90 87 if(str2.length()<=8){
... ... @@ -114,21 +111,36 @@ void slct_command(String&amp; str1,String&amp; str2){
114 111 }else{
115 112 Serial.println("Error Buffer Full");
116 113 }
  114 +
  115 +
  116 + Serial.println("Waiting for acknowledgement...");
  117 + while(arcan.get_message(&message_rec) == 0);
  118 + Serial.println("Acknowledgement received");
117 119 arcan.mode_sleep();
118   - }else{
119   - if(arcan.check_free_buffer()){
120   - num_buffer=arcan.send_message(&message_sent);
121   - Serial.println("Now, send the message with data");
122   - sending=true;
123   - }else{
124   - Serial.println("Error Buffer Full");
125   - }
126   - }
127 120 }
128 121 if(str1=="receive"){
129 122 if(arcan.get_message(&message_rec) != 0){
130 123 arcan.print_message(&message_rec);
131 124 Serial.print("Message received");
  125 +
  126 + //Enviamos mensaje de confirmacion de recepcion
  127 + uint32_t id_fr=(message_rec.id>>5)&0x1F;
  128 + uint32_t id_to=(message_rec.id&0x1F);
  129 + message_sent.id=(id_to<<5)|(0x1F&id_fr);
  130 + message_sent.header.rtr=0;
  131 + message_sent.header.length=0;
  132 +
  133 + if(arcan.check_free_buffer()){
  134 + num_buffer=arcan.send_message(&message_sent);
  135 +
  136 + //Vaciamos buffer de entrada
  137 + while(arcan.get_message(&message_rec)!=0);
  138 +
  139 + if(num_buffer==0) Serial.println("Error sending");
  140 + }else{
  141 + Serial.println("Error Buffer Full");
  142 + }
  143 +
132 144 }else{
133 145 Serial.print("There are no messages in buffer");
134 146 }
... ...
bus_can3/bus_can3.ino 0 → 100644
... ... @@ -0,0 +1,165 @@
  1 +#include <ArCan.h>
  2 +#include <ArCan_def.h>
  3 +
  4 +tArCan::tCAN message_sent;
  5 +tArCan::tCAN message_rec;
  6 +tArCan arcan;
  7 +int num_buffer;
  8 +String string1; String string2;String string3;String string4;
  9 +volatile uint64_t timer1_overflows=0;
  10 +int year;int month;int day;int hour;int minute;int second;
  11 +String aux;String aux2;String aux3;
  12 +uint32_t id;uint32_t mask;uint32_t filter;
  13 +uint32_t id_fr; uint32_t id_to;
  14 +/***Prototipo de las funciones***/
  15 +//Funcion para saber si el comando es valido
  16 +boolean valid_command(String& str1,String& str2);
  17 +//Funcion para saber seleccionar el comando valido introducido
  18 +void slct_command(String& str1,String& str2);
  19 +//Función para incluir el destinatario del mensaje en el campo id
  20 +void include_recipient(String str3);
  21 +
  22 +void setup() {
  23 + // put your setup code here, to run once:
  24 + Serial.begin(9600);
  25 + id_fr=3;
  26 + //1 byte para el id del remitente
  27 + id=id_fr;
  28 + mask=0x0000001F;
  29 + filter=id_fr;
  30 +
  31 + if(arcan.init(id,filter,mask)){
  32 + Serial.println("Starting CANBUS.....");
  33 + }else{
  34 + Serial.println("CANBUS couldn´t start");
  35 + }
  36 + arcan.mode_sleep();
  37 + Serial.println("Commands accepted: "); Serial.println("\"send <message_with_maximum_8_characters> <recipient:1,2_or_3>\"");Serial.println("\"receive\"");Serial.println("\"stime <dd/mm/aa hh:mm:ss>\"");Serial.println("\"gtime\"");Serial.println("\"help\"");
  38 +}
  39 +
  40 +void loop() {
  41 + // put your main code here, to run repeatedly:
  42 + if(Serial.available()>0){
  43 + String command=Serial.readStringUntil('\n');
  44 + command.trim(); command.toLowerCase();
  45 + Serial.print("command: \""); Serial.print(command); Serial.println("\"");
  46 + string1=command.substring(0,command.indexOf(" "));
  47 + string2=command.substring(command.indexOf(" "),command.length());
  48 + string3=command.substring(command.indexOf(" "),command.length()-2);
  49 + string4=command.substring(command.length()-1);
  50 + if(valid_command(string1,string2)){
  51 + if(string1 == "send"){
  52 + slct_command(string1,string3);
  53 + }else{
  54 + slct_command(string1,string2);
  55 + }
  56 + }
  57 + }
  58 +}
  59 +
  60 +boolean valid_command(String& str1,String& str2){
  61 + String str=str1+str2;
  62 + if((str1=="send")||(str1=="receive")||(str1=="help")||(str1=="stime")||(str1=="gtime")){
  63 + Serial.print ("The command \""); Serial.print(str1);Serial.print("\" is successfully executed");Serial.println("\"");
  64 + return true;
  65 + }else{
  66 + Serial.print("[ERROR] bad command \""); Serial.print(str1); Serial.println("\"");
  67 + Serial.print("Commands accepted: "); Serial.println("\"send <message_with_maximum_8_characters> <recipient:1,2_or_3>\"");Serial.println("\"receive\"");Serial.println("\"stime <dd/mm/aa hh:mm:ss>\"");Serial.println("\"gtime\"");
  68 + return false;
  69 + }
  70 +}
  71 +void include_recipient(String str3){
  72 + id_to=(uint32_t)str3.toInt();
  73 + message_sent.id=(id<<5)|(0x1F&id_to);
  74 +}
  75 +void slct_command(String& str1,String& str2){
  76 +
  77 + if(str1=="help"){
  78 + Serial.println("Commands accepted: "); Serial.println("\"send <message_with_maximum_8_characters> <recipient:1,2_or_3>\"");Serial.println("\"receive\"");Serial.println("\"stime <dd/mm/aa hh:mm:ss>\"");Serial.println("\"gtime\"");
  79 + Serial.println("For sending messages: ");
  80 + Serial.println(" 1.- Send a message without data in these format: send <recipient>");
  81 + Serial.println(" 2.- Send a massage with data in these format: send <message_with_maximum_8_characters> <recipient:1,2_or_3>");
  82 + }
  83 + if(str1=="send"){
  84 + include_recipient(string4);
  85 + int count=0;
  86 + //Primer caracter de str2 es un espacio por el trim de antes
  87 + if(str2.length()<=8){
  88 + Serial.print("Message \"");
  89 + for(int i=1;i<str2.length();i++){
  90 + count++;
  91 + message_sent.data[i-1]=str2.charAt(i);
  92 + Serial.print(str2.charAt(i));
  93 + }
  94 + }else{
  95 + Serial.println("The message is too long,it has been truncated.");Serial.println();
  96 + Serial.print("Message \"");
  97 + for(int i=1;i<8;i++){
  98 + count++;
  99 + //i-1 por los espacios, ya que str2 empieza en 1, si empezra en 0 contendria el espacio!!!!
  100 + message_sent.data[i-1]=str2.charAt(i);
  101 + Serial.print(str2.charAt(i));
  102 + }
  103 + }
  104 + //rtr=0 ya que las tramas llevan solamente datos
  105 + message_sent.header.rtr=0;message_sent.header.length=count;
  106 + if(arcan.check_free_buffer()){
  107 + num_buffer=arcan.send_message(&message_sent);
  108 + Serial.print("\" sent");Serial.println();
  109 + arcan.print_message(&message_sent);
  110 + if(num_buffer==0) Serial.println("Error sending");
  111 + }else{
  112 + Serial.println("Error Buffer Full");
  113 + }
  114 +
  115 + Serial.println("Waiting for acknowledgement...");
  116 + while(arcan.get_message(&message_rec) == 0);
  117 + Serial.println("Acknowledgement received");
  118 + arcan.mode_sleep();
  119 + }
  120 + if(str1=="receive"){
  121 + if(arcan.get_message(&message_rec) != 0){
  122 + arcan.print_message(&message_rec);
  123 + Serial.print("Message received");
  124 +
  125 + //Enviamos mensaje de confirmacion de recepcion
  126 + uint32_t id_fr=(message_rec.id>>5)&0x1F;
  127 + uint32_t id_to=(message_rec.id&0x1F);
  128 + message_sent.id=(id_to<<5)|(0x1F&id_fr);
  129 + message_sent.header.rtr=0;
  130 + message_sent.header.length=0;
  131 +
  132 + if(arcan.check_free_buffer()){
  133 + num_buffer=arcan.send_message(&message_sent);
  134 +
  135 + //Vaciamos buffer de entrada
  136 + while(arcan.get_message(&message_rec)!=0);
  137 +
  138 + if(num_buffer==0) Serial.println("Error sending");
  139 + }else{
  140 + Serial.println("Error Buffer Full");
  141 + }
  142 +
  143 + }else{
  144 + Serial.print("There are no messages in buffer");
  145 + }
  146 + }
  147 + if (str1=="stime"){
  148 + day=(str2.substring(0,str2.indexOf("/"))).toInt();
  149 + aux=str2.substring(str2.indexOf("/")+1,str2.length());
  150 + month=(aux.substring(0,aux.indexOf("/"))).toInt();
  151 + aux=aux.substring(aux.indexOf("/")+1,aux.length());
  152 + year=(aux.substring(0,aux.indexOf(" "))).toInt();
  153 + aux=aux.substring(aux.indexOf(" ")+1,aux.length());
  154 + hour=(aux.substring(0,aux.indexOf(":"))).toInt();
  155 + aux=aux.substring(aux.indexOf(":")+1,aux.length());
  156 + minute=(aux.substring(0,aux.indexOf(":"))).toInt();
  157 + second=(aux.substring(aux.indexOf(":")+1,aux.length())).toInt();
  158 + arcan.set_time();
  159 + }
  160 + if (str1=="gtime"){
  161 + arcan.get_time(day,month,year,hour,minute,second);
  162 + }
  163 +}
  164 +
  165 +
... ...