Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: xtoff2 RF24Network mbed
Fork of xtoff3 by
main.cpp
00001 #include "mbed.h" 00002 #include "Transmitter.h" 00003 #include "Maths.h" 00004 #include "LoadCell.h" 00005 00006 Serial pc(USBTX, USBRX); 00007 Ontvanger tx; 00008 LoadCell test(A0); 00009 InterruptIn reedSensor(D9); 00010 Ticker sampler; 00011 Timer t; 00012 00013 00014 int reed = 0; 00015 00016 int DEBUG_ENABLED = 0; 00017 int meet = 0; 00018 float val2[10000]; 00019 int VAL_AM = 0; 00020 00021 00022 const int BEFORE_AMOUNT = 1; 00023 float BEFORE_VALUES[BEFORE_AMOUNT]; 00024 int BEFORE_COUNTER = 0; 00025 00026 const int REED_AMOUNT = 1; 00027 float REED_VALUES[REED_AMOUNT][BEFORE_AMOUNT]; 00028 int REED_COUNTER = 0; 00029 int REED_ENABLE = 0; 00030 00031 int MAGNET_COUNTER = 0; 00032 00033 void log(const char* format, ...) 00034 { 00035 if (DEBUG_ENABLED) { 00036 char buffer[256]; 00037 va_list args; 00038 va_start(args, format); 00039 vsprintf(buffer, format, args); 00040 pc.printf("DEBUG - %s \r\n", buffer); 00041 va_end(args); 00042 } 00043 } 00044 00045 void send(float mass) 00046 { 00047 tx.update(); 00048 payload_t payload; 00049 payload.command = 0x01; 00050 payload.mass = mass; 00051 log("Writing now"); 00052 bool ok = tx.write(payload); 00053 log("Done writing"); 00054 if (ok) 00055 log("Ok"); 00056 else 00057 log("Failed"); 00058 } 00059 00060 00061 void sendCommand (char c) 00062 { 00063 switch (c) { 00064 case 't': 00065 float tare = test.tare(); 00066 log("Tare %f", tare); 00067 send(tare); 00068 break; 00069 case 'T': 00070 float tare2 = test.tareDown(); 00071 log("Tare %f", tare2); 00072 send(tare2); 00073 break; 00074 case 'c': 00075 float cali = test.callibrate(); 00076 log("Callibrate %f", cali); 00077 send(cali); 00078 break; 00079 case 'y': 00080 float volt = test.analogRead(); 00081 log("Voltage %f", volt); 00082 send(volt); 00083 break; 00084 case 'm': 00085 float mass = test.mass(); 00086 log("MASS %f", mass); 00087 send(mass); 00088 break; 00089 case 'M': 00090 float mass2 = test.simpleMass(); 00091 log("MASS %f", mass2); 00092 send(mass2); 00093 break; 00094 case 'P': 00095 for (int i = 0; i<10000; i++) { 00096 send(val2[i]); 00097 wait_ms(25); 00098 } 00099 break; 00100 case 'p': 00101 float test[REED_AMOUNT]; 00102 float test2[REED_AMOUNT]; 00103 00104 for (int i = 0; i<REED_AMOUNT; i++) { 00105 for (int j = 0; j<BEFORE_AMOUNT; j++) { 00106 send(REED_VALUES[i][j]); 00107 printf("%f,",REED_VALUES[i][j]); 00108 } 00109 printf("\r\n"); 00110 send(999); 00111 00112 test[i] = Maths::mean(REED_VALUES[i],0,BEFORE_AMOUNT-1); 00113 test2[i] = Maths::meanNoOutliers(REED_VALUES[i],BEFORE_AMOUNT-1); 00114 } 00115 send(999); 00116 send(Maths::mean(test,0,REED_AMOUNT)); 00117 send(999); 00118 send(Maths::meanNoOutliers(test,REED_AMOUNT)); 00119 send(999); 00120 send(Maths::meanNoOutliers(test2,REED_AMOUNT)); 00121 send(999); 00122 00123 break; 00124 case 'r': 00125 REED_ENABLE = !REED_ENABLE; 00126 memset( REED_VALUES, 0, sizeof( REED_VALUES ) ); 00127 memset( BEFORE_VALUES, 0, sizeof( BEFORE_VALUES) ); 00128 REED_COUNTER = 0; 00129 break; 00130 }; 00131 } 00132 void readNF24() 00133 { 00134 tx.update(); 00135 if (tx.available()) { 00136 payload_t payload; 00137 payload = tx.read(); 00138 pc.printf("%c\r\n", payload.command); 00139 sendCommand(payload.command); 00140 } 00141 } 00142 00143 void reedS() 00144 { 00145 if (REED_ENABLE) 00146 reed = 1; 00147 } 00148 00149 void getSample() 00150 { 00151 if (REED_ENABLE) { 00152 val2[VAL_AM] = test.simpleAnalogRead(); 00153 VAL_AM = (VAL_AM + 1) %10000; 00154 } 00155 } 00156 00157 void init() 00158 { 00159 DEBUG_ENABLED = 0; 00160 log("INITIALISE"); 00161 log("DEBUGGING IS: ON"); 00162 log("Load cell using: Analog Pin A2"); 00163 log("Transmitter using: D4, D3, A1, A6, A5"); 00164 tx.printDetails(); 00165 00166 log("INIT REED SENSOR"); 00167 00168 reedSensor.fall(&reedS); 00169 reedSensor.mode(PullUp); 00170 sampler.attach(&getSample, 0.00004); 00171 00172 } 00173 00174 00175 void receive() 00176 { 00177 while (true) { 00178 pc.printf("");//print niet weg doen, één of andere reden werkt het niet zonder 00179 tx.update(); 00180 if (tx.available()) { 00181 payload_t payload; 00182 payload = tx.read(); 00183 if(!payload.messageAvailable) { 00184 pc.printf("%f\r\n", payload.mass); 00185 } 00186 } 00187 } 00188 } 00189 00190 00191 00192 00193 00194 void readPc() 00195 { 00196 if(pc.readable()) { 00197 sendCommand(pc.getc()); 00198 } 00199 } 00200 00201 void readReed() 00202 { 00203 if(reed) { 00204 /*for (int i = 0; i <BEFORE_AMOUNT ; i++) 00205 REED_VALUES[REED_COUNTER][i] = BEFORE_VALUES[(i+BEFORE_COUNTER) % BEFORE_AMOUNT]; 00206 REED_COUNTER=(REED_COUNTER+1) % REED_AMOUNT; 00207 00208 MAGNET_COUNTER++; 00209 */ 00210 REED_ENABLE = 0; 00211 send(VAL_AM); 00212 reed = 0; 00213 00214 } 00215 } 00216 00217 int main() 00218 { 00219 init(); 00220 while (1) { 00221 //If reed is enabled 00222 if(!REED_ENABLE) { 00223 readPc(); 00224 readNF24(); 00225 } else { 00226 readReed(); 00227 /*val2[VAL_AM] = test.simpleAnalogRead(); 00228 VAL_AM = (VAL_AM + 1) %10000; 00229 */ 00230 /*BEFORE_VALUES[BEFORE_COUNTER] = test.mass(); 00231 BEFORE_COUNTER ++; 00232 BEFORE_COUNTER = BEFORE_COUNTER % BEFORE_AMOUNT; 00233 if(MAGNET_COUNTER >= REED_AMOUNT) { 00234 MAGNET_COUNTER = 0; 00235 REED_ENABLE = !REED_ENABLE; 00236 }*/ 00237 } 00238 } 00239 00240 }
Generated on Tue Jul 19 2022 01:01:54 by
