int main() { int code=5,i=0; double duration,duration1,pos_arr[3][2], tmp1=0, tmp2=0, tmp3=0; struct timeval start,end,start1; RCSwitch mySwitch = RCSwitch(); mySwitch.enableTransmit(PIN); receiver receiver1(1),receiver2(2),receiver3(3); receiver1.init(); receiver2.init(); receiver3.init(); ofstream data; data.open ("data.txt"); printf("activating emitter\n"); mySwitch.send(code,24); // delay(3); while (true) { gettimeofday(&start1,NULL); std::thread t1(&receiver::dst,&receiver1); std::thread t2(&receiver::dst,&receiver2); std::thread t3(&receiver::dst,&receiver3); t1.join(); t2.join(); t3.join(); // std::cout<<receiver1.distance<<" "<<receiver2.distance<<" "<<receiver3.distance<<" "<<i<<std::endl; pos_arr[0][0]=tmp1; pos_arr[1][0]=tmp2; pos_arr[2][0]=tmp3; pos_arr[0][1]=receiver1.distance; pos_arr[1][1]=receiver2.distance; pos_arr[2][1]=receiver3.distance; tmp1= pos_arr[0][1]; tmp2= pos_arr[1][1]; tmp3= pos_arr[2][1]; gettimeofday(&start,NULL); kalman(pos_arr); gettimeofday(&end,NULL); duration1=(abs(end.tv_usec-start1.tv_usec)/1000.0); duration=(abs(end.tv_usec-start.tv_usec)/1000.0); //std::cout<<"receiver1"<<receiver1.distance <<"receiver2"<<receiver2.distance<< "receiver3"<<receiver3.distance << std::endl; std::cout<<"time for Kalman / calculations "<<duration<< " " << duration1<< std::endl; delay(50-duration1); // gettimeofday(&end1,NULL); // duration1=((dend1.tv_usec-start1.tv_usec)*1000+(end1.tv_sec-start1.tv_sec)/1000)+0.5; // std::cout<<"time for Kalman "<<duration<<std::endl; // delay(50-duration1/1000000); i++; } data.close(); return 0; }
int main() { int code=5,i=0; timespec tsleep{0}; long long duration2=0,duration1=0; RCSwitch mySwitch = RCSwitch(); mySwitch.enableTransmit(PIN); receiver receiver1(1),receiver2(2),receiver3(3); receiver1.init(); receiver2.init(); receiver3.init(); printf("activating emitter\n"); mySwitch.send(code,24); std::ofstream data; data.open ("data.txt"); // delay(3); while (true) { auto begin = std::chrono::high_resolution_clock::now(); //////////////////////////////////////////////////////////////////////////////////// // mySwitch.send(code,24); receiver1.dst(); // receiver2.dst(); // receiver3.dst(); data<<receiver1.distance<<" "<<receiver2.distance<<" "<<receiver3.distance<<std::endl; //////////////////////////////////////////////////////////////////////////////// auto end = std::chrono::high_resolution_clock::now(); // data<<end.tv_usec<<"\n"; duration2 = std::chrono::duration_cast <std::chrono::nanoseconds>(end-begin).count(); tsleep.tv_nsec=50000000-duration2; nanosleep(&tsleep,NULL); // i++; // if (i==20) // { // mySwitch.send(code,24); // i=0; // } // auto end2 = std::chrono::high_resolution_clock::now(); // duration1 = std::chrono::duration_cast <std::chrono::nanoseconds>(end2-begin).count(); // data<<duration1<<"\n"; } return 0; }
int main() { int rc; int val; /* Non-blocking receiver case. */ int ch1 = chmake(sizeof(int)); errno_assert(ch1 >= 0); int hndl1 = go(sender1(ch1, 555)); errno_assert(hndl1 >= 0); struct chclause cls1[] = {{CHRECV, ch1, &val, sizeof(val)}}; rc = choose(cls1, 1, -1); choose_assert(0, 0); assert(val == 555); hclose(ch1); rc = hclose(hndl1); errno_assert(rc == 0); /* Blocking receiver case. */ int ch2 = chmake(sizeof(int)); errno_assert(ch2 >= 0); int hndl2 = go(sender2(ch2, 666)); errno_assert(hndl2 >= 0); struct chclause cls2[] = {{CHRECV, ch2, &val, sizeof(val)}}; rc = choose(cls2, 1, -1); choose_assert(0, 0); assert(val == 666); hclose(ch2); rc = hclose(hndl2); errno_assert(rc == 0); /* Non-blocking sender case. */ int ch3 = chmake(sizeof(int)); errno_assert(ch3 >= 0); int hndl3 = go(receiver1(ch3, 777)); errno_assert(hndl3 >= 0); val = 777; struct chclause cls3[] = {{CHSEND, ch3, &val, sizeof(val)}}; rc = choose(cls3, 1, -1); choose_assert(0, 0); hclose(ch3); rc = hclose(hndl3); errno_assert(rc == 0); /* Blocking sender case. */ int ch4 = chmake(sizeof(int)); errno_assert(ch4 >= 0); int hndl4 = go(receiver2(ch4, 888)); errno_assert(hndl4 >= 0); val = 888; struct chclause cls4[] = {{CHSEND, ch4, &val, sizeof(val)}}; rc = choose(cls4, 1, -1); choose_assert(0, 0); hclose(ch4); rc = hclose(hndl4); errno_assert(rc == 0); /* Check with two channels. */ int hndl5[2]; int ch5 = chmake(sizeof(int)); errno_assert(ch5 >= 0); int ch6 = chmake(sizeof(int)); errno_assert(ch6 >= 0); hndl5[0] = go(sender1(ch6, 555)); errno_assert(hndl5 >= 0); struct chclause cls5[] = { {CHRECV, ch5, &val, sizeof(val)}, {CHRECV, ch6, &val, sizeof(val)} }; rc = choose(cls5, 2, -1); choose_assert(1, 0); assert(val == 555); hndl5[1] = go(sender2(ch5, 666)); errno_assert(hndl5 >= 0); rc = choose(cls5, 2, -1); choose_assert(0, 0); assert(val == 666); hclose(ch5); hclose(ch6); rc = hclose(hndl5[0]); errno_assert(rc == 0); rc = hclose(hndl5[1]); errno_assert(rc == 0); /* Test whether selection of in channels is random. */ int ch7 = chmake(sizeof(int)); errno_assert(ch7 >= 0); int ch8 = chmake(sizeof(int)); errno_assert(ch8 >= 0); int hndl6[2]; hndl6[0] = go(feeder(ch7, 111)); errno_assert(hndl6[0] >= 0); hndl6[1] = go(feeder(ch8, 222)); errno_assert(hndl6[1] >= 0); int i; int first = 0; int second = 0; int third = 0; for(i = 0; i != 100; ++i) { struct chclause cls6[] = { {CHRECV, ch7, &val, sizeof(val)}, {CHRECV, ch8, &val, sizeof(val)} }; rc = choose(cls6, 2, -1); errno_assert(rc == 0 || rc == 1); if(rc == 0) { assert(val == 111); ++first; } if(rc == 1) { assert(val == 222); ++second; } int rc = yield(); errno_assert(rc == 0); } assert(first > 1 && second > 1); hclose(hndl6[0]); hclose(hndl6[1]); hclose(ch7); hclose(ch8); /* Test 'otherwise' clause. */ int ch9 = chmake(sizeof(int)); errno_assert(ch9 >= 0); struct chclause cls7[] = {{CHRECV, ch9, &val, sizeof(val)}}; rc = choose(cls7, 1, 0); choose_assert(-1, ETIMEDOUT); hclose(ch9); rc = choose(NULL, 0, 0); choose_assert(-1, ETIMEDOUT); /* Test two simultaneous senders vs. choose statement. */ int ch10 = chmake(sizeof(int)); errno_assert(ch10 >= 0); int hndl7[2]; hndl7[0] = go(sender1(ch10, 888)); errno_assert(hndl7[0] >= 0); hndl7[1] = go(sender1(ch10, 999)); errno_assert(hndl7[1] >= 0); val = 0; struct chclause cls8[] = {{CHRECV, ch10, &val, sizeof(val)}}; rc = choose(cls8, 1, -1); choose_assert(0, 0); assert(val == 888); val = 0; rc = choose(cls8, 1, -1); choose_assert(0, 0); assert(val == 999); hclose(ch10); rc = hclose(hndl7[0]); errno_assert(rc == 0); rc = hclose(hndl7[1]); errno_assert(rc == 0); /* Test two simultaneous receivers vs. choose statement. */ int ch11 = chmake(sizeof(int)); errno_assert(ch11 >= 0); int hndl8[2]; hndl8[0] = go(receiver1(ch11, 333)); errno_assert(hndl8[0] >= 0); hndl8[1] = go(receiver1(ch11, 444)); errno_assert(hndl8[1] >= 0); val = 333; struct chclause cls9[] = {{CHSEND, ch11, &val, sizeof(val)}}; rc = choose(cls9, 1, -1); choose_assert(0, 0); val = 444; rc = choose(cls9, 1, -1); choose_assert(0, 0); hclose(ch11); rc = hclose(hndl8[0]); errno_assert(rc == 0); rc = hclose(hndl8[1]); errno_assert(rc == 0); /* Choose vs. choose. */ int ch12 = chmake(sizeof(int)); errno_assert(ch12 >= 0); int hndl9 = go(choosesender(ch12, 111)); errno_assert(hndl9 >= 0); struct chclause cls10[] = {{CHRECV, ch12, &val, sizeof(val)}}; rc = choose(cls10, 1, -1); choose_assert(0, 0); assert(val == 111); hclose(ch12); rc = hclose(hndl9); errno_assert(rc == 0); /* Test transferring a large object. */ int ch17 = chmake(sizeof(struct large)); errno_assert(ch17 >= 0); int hndl10 = go(sender4(ch17)); errno_assert(hndl9 >= 0); struct large lrg; struct chclause cls14[] = {{CHRECV, ch17, &lrg, sizeof(lrg)}}; rc = choose(cls14, 1, -1); choose_assert(0, 0); hclose(ch17); /* Test that 'in' on done-with channel fires. */ int ch18 = chmake(sizeof(int)); errno_assert(ch18 >= 0); rc = hdone(ch18); errno_assert(rc == 0); struct chclause cls15[] = {{CHRECV, ch18, &val, sizeof(val)}}; rc = choose(cls15, 1, -1); choose_assert(0, EPIPE); hclose(ch18); /* Test expiration of 'deadline' clause. */ int ch21 = chmake(sizeof(int)); errno_assert(ch21 >= 0); int64_t start = now(); struct chclause cls17[] = {{CHRECV, ch21, &val, sizeof(val)}}; rc = choose(cls17, 1, start + 50); choose_assert(-1, ETIMEDOUT); int64_t diff = now() - start; time_assert(diff, 50); hclose(ch21); /* Test unexpired 'deadline' clause. */ int ch22 = chmake(sizeof(int)); errno_assert(ch22 >= 0); start = now(); int hndl11 = go(sender3(ch22, 4444, start + 50)); errno_assert(hndl11 >= 0); struct chclause cls18[] = {{CHRECV, ch22, &val, sizeof(val)}}; rc = choose(cls17, 1, start + 1000); choose_assert(0, 0); assert(val == 4444); diff = now() - start; time_assert(diff, 50); hclose(ch22); rc = hclose(hndl11); errno_assert(rc == 0); /* Test that first channel in the array is prioritized. */ int ch23 = chmake(sizeof(int)); errno_assert(ch23 >= 0); int ch24 = chmake(sizeof(int)); errno_assert(ch24 >= 0); int hndl12 = go(sender1(ch23, 0)); errno_assert(hndl12 >= 0); int hndl13 = go(sender1(ch24, 0)); errno_assert(hndl13 >= 0); struct chclause cls19[] = { {CHRECV, ch24, &val, sizeof(val)}, {CHRECV, ch23, &val, sizeof(val)} }; rc = choose(cls19, 2, -1); choose_assert(0, 0); rc = chrecv(ch23, &val, sizeof(val), -1); errno_assert(rc == 0); rc = hclose(hndl13); errno_assert(rc == 0); rc = hclose(hndl12); errno_assert(rc == 0); rc = hclose(ch24); errno_assert(rc == 0); rc = hclose(ch23); errno_assert(rc == 0); /* Try adding the same channel to choose twice. Doing so is pointless, but it shouldn't crash the application. */ int ch25 = chmake(sizeof(int)); errno_assert(ch25 >= 0); struct chclause cls20[] = { {CHRECV, ch25, &val, sizeof(val)}, {CHRECV, ch25, &val, sizeof(val)} }; rc = choose(cls20, 2, now() + 50); choose_assert(-1, ETIMEDOUT); rc = hclose(ch25); errno_assert(rc == 0); return 0; }
int sc_main(int argc, char *argv[]) { sc_signal<pkt> pkt_in0; sc_signal<pkt> pkt_in1; sc_signal<pkt> pkt_in2; sc_signal<pkt> pkt_in3; sc_signal<pkt> pkt_out0; sc_signal<pkt> pkt_out1; sc_signal<pkt> pkt_out2; sc_signal<pkt> pkt_out3; sc_signal<sc_int<4> > id0, id1, id2, id3; sc_signal<bool> switch_cntrl; sc_clock clock1("CLOCK1", 75, SC_NS, 0.5, 0.0, SC_NS); sc_clock clock2("CLOCK2", 30, SC_NS, 0.5, 10.0, SC_NS); // Module instiatiations follow // Note that modules can be connected by hooking up ports // to signals by name or by using a positional notation sender sender0("SENDER0"); // hooking up signals to ports by name sender0.pkt_out(pkt_in0); sender0.source_id(id0); sender0.CLK(clock1); sender sender1("SENDER1"); // hooking up signals to ports by position sender1(pkt_in1, id1, clock1); sender sender2("SENDER2"); // hooking up signals to ports by name sender2.pkt_out(pkt_in2); sender2.source_id(id2); sender2.CLK(clock1); sender sender3("SENDER3"); // hooking up signals to ports by position sender3( pkt_in3, id3, clock1 ); switch_clk switch_clk1("SWITCH_CLK"); // hooking up signals to ports by name switch_clk1.switch_cntrl(switch_cntrl); switch_clk1.CLK(clock2); mcast_pkt_switch switch1("SWITCH"); // hooking up signals to ports by name switch1.switch_cntrl(switch_cntrl); switch1.in0(pkt_in0); switch1.in1(pkt_in1); switch1.in2(pkt_in2); switch1.in3(pkt_in3); switch1.out0(pkt_out0); switch1.out1(pkt_out1); switch1.out2(pkt_out2); switch1.out3(pkt_out3); receiver receiver0("RECEIVER0"); // hooking up signals to ports by name receiver0.pkt_in(pkt_out0); receiver0.sink_id(id0); receiver receiver1("RECEIVER1"); // hooking up signals to ports by position receiver1( pkt_out1, id1 ); receiver receiver2("RECEIVER2"); // hooking up signals to ports by name receiver2.pkt_in(pkt_out2); receiver2.sink_id(id2); receiver receiver3("RECEIVER3"); // hooking up signals to ports by position receiver3( pkt_out3, id3 ); sc_start(0, SC_NS); #if !defined(__SUNPRO_CC) id0.write(0); id1.write(1); id2.write(2); id3.write(3); #else // you cannot do that with SC5.0 // since it doesn't support member templates id0.write(sc_int<4>(0)); id0.write(sc_int<4>(1)); id0.write(sc_int<4>(2)); id0.write(sc_int<4>(3)); #endif sc_start(); return 0; }