1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Lab 3 kết nối hai telosb mote

45 357 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 45
Dung lượng 63,67 KB

Nội dung

Báo Cáo Công Nghệ IoT Hiện Đại LAB3: Kết nối hai TelosB MoteMục lụcI.TrafficLight1 (4đ)2Bài giải3II.TrafficLight3 (2đ)12Bài giải12III.TrafficLight2 (2đ)22Bài giải22IV.TrafficLight4 (2đ)33Bài giải34 I.TrafficLight1 (4đ)Sử dụng 2 telosb motes mô phỏng hệ thống đèn tại ngã tư hoạt động như sau:Thời gian (s)Node0 (master)Node1 (slave)5151Hướng dẫn: Xây dựng một ứng dụng chung cho cả 2 mote, sử dụng TOS_NODE_ID để phân biệt master và slave.Thiết đặt TOS_NODE_ID cho telosb mote tại bước nạp code vào motemake telosb reinstall, bsl,devttyUSB0Sử dụng thêm biến counter để xử lý bậttắt đèn theo thời gian.Các thao tác send message trong event MilliTimer.fired() chỉ nên được thực hiện tại master (kiểm tra TOS_NODE_ID trước khi cho thực hiện nội dung bên trong MilliTimer.fired()).Các thao tác xử lý message nhận được trong event Receive.receive() chỉ nên được thực hiện tại slave (kiểm tra TOS_NODE_ID trước khi cho thực hiện nội dung bên trong Receive.receive()).Nên xây dựng một hàm riêng để xử lý bậttắt đèn. VD: void processLeds(uint16_t counter){…}. Hàm này có thể được gọi trong MilliTimer.fired() (đối với node master) và trong Receive.receive() (đối với node slave). Cách khai báo một hàm mới trong component:void processLeds(uint16_t counter);…void processLeds(uint16_t counter){ … }Bài giảiFile RadioCountToLeds.h Copyright (c) 20042005 The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation andor other materials provided with the distribution. Neither the name of the University of California nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Copyright (c) 20022003 Intel Corporation All rights reserved. This file is distributed under the terms in the attached INTELLICENSE file. If you do not find these files, copies can be found by writing to Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 94704. Attention: Intel License Inquiry. ifndef RADIO_COUNT_TO_LEDS_Hdefine RADIO_COUNT_TO_LEDS_Htypedef nx_struct radio_count_msg { nx_uint16_t counter; nx_uint16_t master_id;} radio_count_msg_t;enum { AM_RADIO_COUNT_MSG = 6,};endifFile RadioCountToLedsAppC.nc Id: RadioCountToLedsAppC.nc,v 1.5 20100629 22:07:17 scipio Exp Copyright (c) 20002005 The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation andor other materials provided with the distribution. Neither the name of the University of California nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Copyright (c) 20022003 Intel Corporation All rights reserved. This file is distributed under the terms in the attached INTELLICENSE file. If you do not find these files, copies can be found by writing to Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 94704. Attention: Intel License Inquiry. include RadioCountToLeds.h Configuration for the RadioCountToLeds application. RadioCountToLeds maintains a 4Hz counter, broadcasting its value in an AM packet every time it gets updated. A RadioCountToLeds node that hears a counter displays the bottom three bits on its LEDs. This application is a useful test to show that basic AM communication and timers work. author Philip Levis date June 6 2005 configuration RadioCountToLedsAppC {}implementation { components MainC, RadioCountToLedsC as App, LedsC; components new AMSenderC(AM_RADIO_COUNT_MSG); components new AMReceiverC(AM_RADIO_COUNT_MSG); components new TimerMilliC(); components ActiveMessageC; App.Boot > MainC.Boot; App.Receive > AMReceiverC; App.AMSend > AMSenderC; App.AMControl > ActiveMessageC; App.Leds > LedsC; App.MilliTimer > TimerMilliC; App.Packet > AMSenderC;}File RadioCountToLedsC.ncinclude Timer.hinclude RadioCountToLeds.hmodule RadioCountToLedsC safe() { uses { interface Leds; interface Boot; interface Receive; interface AMSend; interface Timer as MilliTimer; interface SplitControl as AMControl; interface Packet; }}implementation { message_t packet; bool locked; uint16_t counter = 0; void processLeds(uint16_t counter); event void Boot.booted() { call AMControl.start(); } event void AMControl.startDone(error_t err) { if (err == SUCCESS) { call MilliTimer.startPeriodic(1000); } else { call AMControl.start(); } } event void AMControl.stopDone(error_t err) { do nothing } event void MilliTimer.fired() { if(TOS_NODE_ID = 0) return; counter++; dbg(RadioCountToLedsC, RadioCountToLedsC: timer fired, counter is %hu., counter); if (locked) { return; } else { radio_count_msg_t rcm = (radio_count_msg_t)call Packet.getPayload(packet, sizeof(radio_count_msg_t)); if (rcm == NULL) {return; } rcm>counter = counter; if (call AMSend.send(AM_BROADCAST_ADDR, packet, sizeof(radio_count_msg_t)) == SUCCESS) {dbg(RadioCountToLedsC, RadioCountToLedsC: packet sent., counter);locked = TRUE; }processLeds(rcm>counter); } if(counter >= 12) counter =0; } event message_t Receive.receive(message_t bufPtr, void payload, uint8_t len) { if(TOS_NODE_ID == 0) return; dbg(RadioCountToLedsC, Received packet of length %hhu., len); if (len = sizeof(radio_count_msg_t)) {return bufPtr;} else { radio_count_msg_t rcm = (radio_count_msg_t)payload; processLeds(rcm>counter); return bufPtr; } } event void AMSend.sendDone(message_t bufPtr, error_t error) { if (packet == bufPtr) { locked = FALSE; } } void processLeds(uint16_t counter){ if(counter == 1){ if(TOS_NODE_ID == 0){ call Leds.led0On(); call Leds.led1Off(); } else{ call Leds.led2On(); call Leds.led0Off(); } } if(counter == 6){ if(TOS_NODE_ID = 0){ call Leds.led1On(); call Leds.led2Off(); } } if(counter == 7){ if(TOS_NODE_ID == 0){ call Leds.led2On(); call Leds.led0Off(); } else{ call Leds.led0On(); call Leds.led1Off(); } } if(counter == 12){ if(TOS_NODE_ID == 0){ call Leds.led1On(); call Leds.led2Off(); } } }}II.TrafficLight3 (2đ)Sử dụng 3 telosb motes mô phỏng hệ thống đèn tại ngã sáu hoạt động như sau:Thời gian (s)Node0 (master)Node1 (slave)Node2(slave)515131Bài giảiFile RadioCountToLeds.h Copyright (c) 20042005 The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation andor other materials provided with the distribution. Neither the name of the University of California nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Copyright (c) 20022003 Intel Corporation All rights reserved. This file is distributed under the terms in the attached INTELLICENSE file. If you do not find these files, copies can be found by writing to Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 94704. Attention: Intel License Inquiry. ifndef RADIO_COUNT_TO_LEDS_Hdefine RADIO_COUNT_TO_LEDS_Htypedef nx_struct radio_count_msg { nx_uint16_t counter;} radio_count_msg_t;enum { AM_RADIO_COUNT_MSG = 6,};endifFile RadioCountToLedsAppC.nc Id: RadioCountToLedsAppC.nc,v 1.5 20100629 22:07:17 scipio Exp Copyright (c) 20002005 The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation andor other materials provided with the distribution. Neither the name of the University of California nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Copyright (c) 20022003 Intel Corporation All rights reserved. This file is distributed under the terms in the attached INTELLICENSE file. If you do not find these files, copies can be found by writing to Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 94704. Attention: Intel License Inquiry. include RadioCountToLeds.h Configuration for the RadioCountToLeds application. RadioCountToLeds maintains a 4Hz counter, broadcasting its value in an AM packet every time it gets updated. A RadioCountToLeds node that hears a counter displays the bottom three bits on its LEDs. This application is a useful test to show that basic AM communication and timers work. author Philip Levis date June 6 2005 configuration RadioCountToLedsAppC {}implementation { components MainC, RadioCountToLedsC as App, LedsC; components new AMSenderC(AM_RADIO_COUNT_MSG); components new AMReceiverC(AM_RADIO_COUNT_MSG); components new TimerMilliC(); components ActiveMessageC; App.Boot > MainC.Boot; App.Receive > AMReceiverC; App.AMSend > AMSenderC; App.AMControl > ActiveMessageC; App.Leds > LedsC; App.MilliTimer > TimerMilliC; App.Packet > AMSenderC;}File RadioCountToLedsC.ncinclude Timer.hinclude RadioCountToLeds.hmodule RadioCountToLedsC safe() { uses { interface Leds; interface Boot; interface Receive; interface AMSend; interface Timer as MilliTimer; interface SplitControl as AMControl; interface Packet; }}implementation { message_t packet; bool locked; uint16_t counter1 = 0; void processLeds(uint16_t counter); event void Boot.booted() { call AMControl.start(); } event void AMControl.startDone(error_t err) { if (err == SUCCESS) { call MilliTimer.startPeriodic(1000); } else { call AMControl.start(); } } event void AMControl.stopDone(error_t err) { do nothing }

Báo Cáo Công Nghệ IoT Hiện Đại LAB3: Kết nối hai TelosB Mote Mục lục I TrafficLight1 (4đ) Sử dụng telosb motes mô hệ thống đèn ngã tư hoạt động sau: Thời gian (s) 5 Node0 (master) Node1 (slave) Hướng dẫn: - Xây dựng ứng dụng chung cho mote, sử dụng TOS_NODE_ID để phân biệt master slave - Thiết đặt TOS_NODE_ID cho telosb mote bước nạp code vào mote make telosb reinstall, bsl,/dev/ttyUSB0 - Sử dụng thêm biến counter để xử lý bật/tắt đèn theo thời gian - Các thao tác send message event MilliTimer.fired() nên thực master (kiểm tra TOS_NODE_ID trước cho thực nội dung bên MilliTimer.fired()) - Các thao tác xử lý message nhận event Receive.receive() nên thực slave (kiểm tra TOS_NODE_ID trước cho thực nội dung bên Receive.receive()) - Nên xây dựng hàm riêng để xử lý bật/tắt đèn VD: void processLeds(uint16_t counter) {…} Hàm gọi MilliTimer.fired() (đối với node master) Receive.receive() (đối với node slave) Cách khai báo hàm component: void processLeds(uint16_t counter); … void processLeds(uint16_t counter) { … } Bài giải File RadioCountToLeds.h /* * Copyright (c) 2004-2005 The Regents of the University of California * All rights reserved * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution * - Neither the name of the University of California nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE * * Copyright (c) 2002-2003 Intel Corporation * All rights reserved * * This file is distributed under the terms in the attached INTEL-LICENSE * file If you not find these files, copies can be found by writing to * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, * 94704 Attention: Intel License Inquiry */ #ifndef RADIO_COUNT_TO_LEDS_H #define RADIO_COUNT_TO_LEDS_H typedef nx_struct radio_count_msg { nx_uint16_t counter; nx_uint16_t master_id; } radio_count_msg_t; enum { AM_RADIO_COUNT_MSG = 6, }; #endif File RadioCountToLedsAppC.nc // $Id: RadioCountToLedsAppC.nc,v 1.5 2010-06-29 22:07:17 scipio Exp $ /* * Copyright (c) 2000-2005 The Regents of the University of California * All rights reserved * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution * - Neither the name of the University of California nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE * * Copyright (c) 2002-2003 Intel Corporation * All rights reserved * * This file is distributed under the terms in the attached INTEL-LICENSE * file If you not find these files, copies can be found by writing to * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, * 94704 Attention: Intel License Inquiry */ #include "RadioCountToLeds.h" /** * Configuration for the RadioCountToLeds application RadioCountToLeds * maintains a 4Hz counter, broadcasting its value in an AM packet * every time it gets updated A RadioCountToLeds node that hears a counter * displays the bottom three bits on its LEDs This application is a useful * test to show that basic AM communication and timers work * * @author Philip Levis * @date June 2005 */ configuration RadioCountToLedsAppC {} implementation { components MainC, RadioCountToLedsC as App, LedsC; components new AMSenderC(AM_RADIO_COUNT_MSG); components new AMReceiverC(AM_RADIO_COUNT_MSG); components new TimerMilliC(); components ActiveMessageC; App.Boot -> MainC.Boot; App.Receive -> AMReceiverC; App.AMSend -> AMSenderC; App.AMControl -> ActiveMessageC; App.Leds -> LedsC; App.MilliTimer -> TimerMilliC; App.Packet -> AMSenderC; } File RadioCountToLedsC.nc #include "Timer.h" #include "RadioCountToLeds.h" module RadioCountToLedsC @safe() { uses { interface Leds; interface Boot; interface Receive; interface AMSend; interface Timer as MilliTimer; interface SplitControl as AMControl; interface Packet; } } implementation { message_t packet; bool locked; uint16_t counter = 0; void processLeds(uint16_t counter); event void Boot.booted() { call AMControl.start(); } event void AMControl.startDone(error_t err) { if (err == SUCCESS) { call MilliTimer.startPeriodic(1000); } else { call AMControl.start(); } } event void AMControl.stopDone(error_t err) { // nothing } event void MilliTimer.fired() { if(TOS_NODE_ID != 0) return; counter++; dbg("RadioCountToLedsC", "RadioCountToLedsC: timer fired, counter is %hu.\n", counter); if (locked) { return; } else { radio_count_msg_t* rcm = Packet.getPayload(&packet, sizeof(radio_count_msg_t)); (radio_count_msg_t*)call if (rcm == NULL) { return; } rcm->counter = counter; if (call AMSend.send(AM_BROADCAST_ADDR, sizeof(radio_count_msg_t)) == SUCCESS) { dbg("RadioCountToLedsC", "RadioCountToLedsC: counter); locked = TRUE; } packet &packet, sent.\n", processLeds(rcm->counter); } if(counter >= 12) counter =0; } event message_t* Receive.receive(message_t* bufPtr, void* payload, uint8_t len) { if(TOS_NODE_ID == 0) return; dbg("RadioCountToLedsC", "Received packet of length %hhu.\n", len); if (len != sizeof(radio_count_msg_t)) {return bufPtr;} else { radio_count_msg_t* rcm = (radio_count_msg_t*)payload; processLeds(rcm->counter); return bufPtr; } } event void AMSend.sendDone(message_t* bufPtr, error_t error) { if (&packet == bufPtr) { locked = FALSE; } } void processLeds(uint16_t counter){ if(counter == 1){ 10 expiration_time = 20; if(TOS_NODE_ID == 0) return; dbg("RadioCountToLedsC", "Received packet of length %hhu.\n", len); if (len != sizeof(radio_count_msg_t)) {return bufPtr;} else { radio_count_msg_t* rcm = (radio_count_msg_t*)payload; processLeds(rcm->counter); return bufPtr; } } event void AMSend.sendDone(message_t* bufPtr, error_t error) { if (&packet == bufPtr) { locked = FALSE; } } void processLeds(uint16_t counter){ if(counter == 1){ if(TOS_NODE_ID == 0){ call Leds.led0On(); call Leds.led1Off(); } else{ call Leds.led2On(); 31 call Leds.led0Off(); } } if(counter == 6){ if(TOS_NODE_ID != 0){ call Leds.led1On(); call Leds.led2Off(); } } if(counter == 7){ if(TOS_NODE_ID == 0){ call Leds.led2On(); call Leds.led0Off(); } else{ call Leds.led0On(); call Leds.led1Off(); } } if(counter == 12){ if(TOS_NODE_ID == 0){ call Leds.led1On(); call Leds.led2Off(); } } 32 } } IV TrafficLight4 (2đ) Dựa TrafficLight3, sinh viên phát triển thêm để xử lý trường hợp: master node ngưng hoạt động (slave chờ 20s liên tục không nhận message từ master), slave tự promote thân thành master điều khiển toàn hệ thống Khi master cũ hoạt động trở lại hoạt động vai trò slave lắng nghe điều khiển từ master Video demo: https://youtu.be/JK_nCLRXpyA Gợi ý: - Chương trình nên có biến master_id lưu trữ id node master hệ thống (mặc định master_id = 0) - Để tránh việc xung đột/tranh giành promote lên master, slave nên có thời gian đợi waiting_time khác (VD: waiting_time = 20 + TOS_NODE_ID * 5) Khi đó, node có thời gian chờ tối đa khác thứ tự ưu tiên promote theo thứ tự node0, node1, node2 - Khi slave X promote thân lên thành master slave X phải thơng báo master_id cho tồn hệ thống cập nhật -> cần bổ sung thuộc tính master_id vào typedef nx_struct radio_count_msg - Để tránh việc master cũ sau hoạt động trở lại chưa kịp cập nhật thông tin master_id (tránh việc master cũ gửi message vào hệ thống gây rối loạn có master gửi message), master boot cần có thêm thời gian chờ waiting_for_the_first_time = 10 s Trong thời gian 10s sau boot, node lắng nghe message từ hệ thống Bài giải File RadioCountToLeds.h 33 /* * Copyright (c) 2004-2005 The Regents of the University of California * All rights reserved * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution * - Neither the name of the University of California nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE * * Copyright (c) 2002-2003 Intel Corporation * All rights reserved * * This file is distributed under the terms in the attached INTEL-LICENSE * file If you not find these files, copies can be found by writing to * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, * 94704 Attention: Intel License Inquiry */ #ifndef RADIO_COUNT_TO_LEDS_H #define RADIO_COUNT_TO_LEDS_H typedef nx_struct radio_count_msg { nx_uint16_t counter; 34 } radio_count_msg_t; enum { AM_RADIO_COUNT_MSG = 6, }; #endif File RadioCountToLedsAppC.nc // $Id: RadioCountToLedsAppC.nc,v 1.5 2010-06-29 22:07:17 scipio Exp $ /* * Copyright (c) 2000-2005 The Regents of the University of California * All rights reserved * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution * - Neither the name of the University of California nor the names of * its contributors may be used to endorse or promote products derived 35 * from this software without specific prior written permission * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE * * Copyright (c) 2002-2003 Intel Corporation * All rights reserved * 36 * This file is distributed under the terms in the attached INTEL-LICENSE * file If you not find these files, copies can be found by writing to * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, * 94704 Attention: Intel License Inquiry */ #include "RadioCountToLeds.h" /** * Configuration for the RadioCountToLeds application RadioCountToLeds * maintains a 4Hz counter, broadcasting its value in an AM packet * every time it gets updated A RadioCountToLeds node that hears a counter * displays the bottom three bits on its LEDs This application is a useful * test to show that basic AM communication and timers work * * @author Philip Levis * @date June 2005 */ configuration RadioCountToLedsAppC {} implementation { components MainC, RadioCountToLedsC as App, LedsC; components new AMSenderC(AM_RADIO_COUNT_MSG); components new AMReceiverC(AM_RADIO_COUNT_MSG); components new TimerMilliC(); components ActiveMessageC; 37 App.Boot -> MainC.Boot; App.Receive -> AMReceiverC; App.AMSend -> AMSenderC; App.AMControl -> ActiveMessageC; App.Leds -> LedsC; App.MilliTimer -> TimerMilliC; App.Packet -> AMSenderC; } File RadioCountToLedsC.nc #include "Timer.h" #include "RadioCountToLeds.h" module RadioCountToLedsC @safe() { uses { interface Leds; interface Boot; interface Receive; interface AMSend; interface Timer as MilliTimer; interface SplitControl as AMControl; interface Packet; } } implementation { 38 message_t packet; int waiting_time0 = 20; int waiting_time1 = 25; int waiting_time2 = 30; uint16_t master_id1 = 0; bool locked; int temp = 10; uint16_t counter1 = 0; void processLeds(uint16_t counter); event void Boot.booted() { call AMControl.start(); temp = 10; } event void AMControl.startDone(error_t err) { if (err == SUCCESS) { call MilliTimer.startPeriodic(1000); } else { call AMControl.start(); } } event void AMControl.stopDone(error_t err) { // nothing 39 } event void MilliTimer.fired() { if(temp != 0){ temp ; } else{ if(TOS_NODE_ID != master_id1){ waiting_time0 ; waiting_time1 ; waiting_time2 ; if(waiting_time0 < 0) waiting_time0 = 0; if(waiting_time1 < 0) waiting_time1 = 0; if(waiting_time2 < 0) waiting_time2 = 0; if((waiting_time0 == 0) && (master_id1 != 0)){ master_id1 = 0; counter1 = 0; call Leds.led0Off(); call Leds.led1Off(); call Leds.led2Off(); } if((waiting_time1 == 0) && (master_id1 != 1)){ master_id1 = 1; counter1 = 0; call Leds.led0Off(); call Leds.led1Off(); 40 call Leds.led2Off(); } if((waiting_time2 == 0) && (master_id1 != 2)){ master_id1 = 2; counter1 = 0; call Leds.led0Off(); call Leds.led1Off(); call Leds.led2Off(); } } if(TOS_NODE_ID != master_id1) return; counter1++; dbg("RadioCountToLedsC", "RadioCountToLedsC: timer fired, counter is %hu.\n", counter); if (locked) { return; } else { radio_count_msg_t* rcm = (radio_count_msg_t*)call Packet.getPayload(&packet, sizeof(radio_count_msg_t)); if (rcm == NULL) { return; } rcm->counter = counter1; 41 rcm->master_id = master_id1; if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(radio_count_msg_t)) == SUCCESS) { dbg("RadioCountToLedsC", "RadioCountToLedsC: packet sent.\n", counter); locked = TRUE; processLeds(rcm->counter); } } if(counter1 >= 16) counter1 =0; } } event message_t* Receive.receive(message_t* bufPtr, void* payload, uint8_t len) { waiting_time0 = 20; waiting_time1 = 25; waiting_time2 = 30; dbg("RadioCountToLedsC", "Received packet of length %hhu.\n", len); if (len != sizeof(radio_count_msg_t)) {return bufPtr;} else { radio_count_msg_t* rcm = (radio_count_msg_t*)payload; uint16_t master_temp; master_temp = rcm->master_id; master_id1 = master_temp; 42 if(TOS_NODE_ID == master_temp) return; processLeds(rcm->counter); return bufPtr; } } event void AMSend.sendDone(message_t* bufPtr, error_t error) { if (&packet == bufPtr) { locked = FALSE; } } void processLeds(uint16_t counter){ if(counter == 1){ if(TOS_NODE_ID == 0){ call Leds.led2On(); call Leds.led0Off(); } else if(TOS_NODE_ID == 1){ call Leds.led0On(); } else if(TOS_NODE_ID == 2){ call Leds.led0On(); call Leds.led1Off(); } } 43 if(counter == 6){ if(TOS_NODE_ID == 0){ call Leds.led1On(); call Leds.led2Off(); } } if(counter == 7){ if(TOS_NODE_ID == 0){ call Leds.led0On(); call Leds.led1Off(); } else if(TOS_NODE_ID == 1){ call Leds.led2On(); call Leds.led0Off(); } } if(counter == 12){ if(TOS_NODE_ID == 1){ call Leds.led1On(); call Leds.led2Off(); } } if(counter == 13){ if(TOS_NODE_ID == 1){ call Leds.led0On(); call Leds.led1Off(); } 44 else if(TOS_NODE_ID == 2){ call Leds.led2On(); call Leds.led0Off(); } } if(counter == 16){ if(TOS_NODE_ID == 2){ call Leds.led1On(); call Leds.led2Off(); } } } } 45 ... ActiveMessageC; App.Boot -> MainC.Boot; App.Receive -> AMReceiverC; App.AMSend -> AMSenderC; App.AMControl -> ActiveMessageC; App.Leds -> LedsC; App.MilliTimer -> TimerMilliC; App.Packet -> AMSenderC; }... ActiveMessageC; App.Boot -> MainC.Boot; App.Receive -> AMReceiverC; App.AMSend -> AMSenderC; 16 App.AMControl -> ActiveMessageC; App.Leds -> LedsC; App.MilliTimer -> TimerMilliC; App.Packet -> AMSenderC;... ActiveMessageC; App.Boot -> MainC.Boot; 26 App.Receive -> AMReceiverC; App.AMSend -> AMSenderC; App.AMControl -> ActiveMessageC; App.Leds -> LedsC; App.MilliTimer -> TimerMilliC; App.Packet -> AMSenderC;

Ngày đăng: 11/04/2019, 23:56

TỪ KHÓA LIÊN QUAN

w