From: Andy Spencer Date: Thu, 13 Feb 2014 00:29:21 +0000 (+0000) Subject: Finish up X-Git-Url: http://pileus.org/git/?p=~andy%2Fcsm213a-hw;a=commitdiff_plain;h=d79f24cc277e31d7848f9683bac599339348aa44 Finish up --- diff --git a/.gitignore b/.gitignore index eec85d6..1f72ef6 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.o *.pyc *.swp +*.zip *~ __pycache__ xively diff --git a/hw1/report.docx b/hw1/report.docx index 4078b06..9ad42d6 100644 Binary files a/hw1/report.docx and b/hw1/report.docx differ diff --git a/hw1/report.htm b/hw1/report.htm new file mode 100644 index 0000000..9a869d7 --- /dev/null +++ b/hw1/report.htm @@ -0,0 +1,158 @@ + + +

Brief Description

+ +

The objective of this homework is to explore organizing embedded software + using mbed as the platform in context of a simple system. Sensor data flows + in the direction of: mbed → PC → Xively.

+ +

Embedded Target Design

+ +

The mbed code is organized in a single main.cpp file which invokes libraries + to read the sensor data. The Accelerometer, Magnetometer, and Touch Sensor + libraries were acquired through the mbed website and the Analog Input and + Output libraries were included in the mbed SDK.

+ +

The software consists of preemptable sensor loop running at a fixed rate, a + serial data receive interrupt handler, and a direct-memory access serial + transmitter. On each iteration of the sensor loop a time counter is checked + to determine which sensor are due for reading. Each sensor function polls + data from the sensor, packs the data into an output frame, and then write + the frame to the output queue.

+ +

Note: the sensor reading was originally performed in a timer interrupt + routine, however, it moved to a background task to minimize interference + with the reception of serial commands.

+ +

Data Logger Design

+ +The data logger is implemented as a Graphical User Interface using Python and +the GTK+ Toolkit. The source code is organized around the following classes: + + + +

Data Collection

+ +

The mbed FRDM-KL46Z is responsible for data collection and sending it to the + host via Serial-over-USB. Sensors data is collected at a periodic rate and + is transmitted to the PC using the DATA_FRAME format shown below. The Serial + data transmit is handled by the KL46Z Direct-Memory Access (DMA) controller + so that data transmission does not block the CPU. When sensor data is ready, + it is packed and appended to a transmit buffer. At the end of the periodic + loop the DMA controller is enabled and data transmission begins

+ +

Commands are sent to the mbed using the COMMAND_FRAME format and are + processed using a serial interrupt handler. The handler parses the incoming + messages and performs command processing when a valid COMMAND_FRAME is + received.

+ +

Serial Message Formats

+ +

As is mentioned above, the mbed and communicate via UART, thus working based + on serial protocol. Since the mbed can be perceived as a slave to the PC, we + let the mbed collect and send DATA_FRAME, while PC is responsible for + generating COMMAND_FRAME. The frame structure is as follows:

+ +

Data Frame

+ + + +
1 Byte4 bits 4 bits 1 Byte (Varied Size)1 Byte
HEADERdataTypesnsTypedataNumdata TAIL
+ +

Field Descriptions

+ + HEADER: + + + + + +
0x02, constant, indicating the start of a frame
dataType:Data Type: + + + +
0- int81 - int162 - int32
3- uint84 - uint165 - uint32
6- float7 - double
snsType: Sensor Type:
    +
  • 0 - LIGHT_SENSOR
  • +
  • 1 - ACC_SENSOR
  • +
  • 2 - MAG_SENSOR
  • +
  • 3 - TOUCH_SENSOR
  • +
  • 4 - ADC_SENSOR
  • +
dataNum: Data number. Number of data pack in the frame
data: Sensor data. How it is packed is based on dataType and dataNum
TAIL: 0x0A, constant, indicating the end of a frame
+ +

Command Frame

+ + + +
1 Byte4 bits 4 bits 4 Byte 1 Byte
HEADERcmdTypesnsTypeIntervalTAIL
+ +

Field Descriptions

+ + + + + +
HEADER: 0x02, constant, indicating the start of a frame
cmdType: Command Type:
    +
  • 0 - START
  • +
  • 1 - STOP
  • +
  • 2 - SET_INT
  • +
Interval:sampling rate. Representing a float of 4 Bytes.
TAIL: 0x0A, constant, indicating the end of a frame
+ + + +

Sampling Rate

+ +

The following sampling rates were obtained for each sensor, and a combined + sampling rate when all sensors are sampling simultaneously. Data collection + was performed using a 230400 serial baud rate.

+ + + + + + + + + +
Sensor Sampling Rate
Accelerometer 560 Hz
Magnetometer 530 Hz
Touch Sensor 1930 Hz
Light Sensor 2414 Hz
Analog to Digital 820 Hz
All Combined 150 Hz
+ +

Online Submission

+ +
+
Code for mbed
+
http://mbed.org/users/andy753421/code/Sensors/
+ +
Code for PC
+
https://sites.google.com/a/g.ucla.edu/ibeaconnav/homework1/python.zip
+ + +
Video Link
+
(we will do a demo during office hours)
+
diff --git a/vis/logger.py b/vis/logger.py index bd394a1..a1cccf7 100644 --- a/vis/logger.py +++ b/vis/logger.py @@ -37,7 +37,7 @@ class Logger: self.queue.append(state) delta = datetime.utcnow() - self.last if delta.total_seconds() > self.config.maxrate: - self.flush() + return self.flush() # Private methods def flush(self): @@ -54,14 +54,26 @@ class Logger: if not self.running() or len(self.queue) == 0: return + # Limit data to 500 samples per upload + limit = 500 + upload = self.queue + if len(upload) > limit: + print("upload: cropping - %d -> %d" % + (len(upload), limit)) + upload = upload[0:limit-1] + self.feed.datastreams = [ - get(self.queue, 'acc'), - get(self.queue, 'mag'), - get(self.queue, 'tch'), - get(self.queue, 'lgt'), - get(self.queue, 'a2d'), + get(upload, 'acc'), + get(upload, 'mag'), + get(upload, 'tch'), + get(upload, 'lgt'), + get(upload, 'a2d'), ] - self.feed.update() + + try: + self.feed.update() + except Exception as ex: + return str(ex) self.last = datetime.utcnow() self.queue = [] diff --git a/vis/makefile b/vis/makefile index a866541..306cb36 100644 --- a/vis/makefile +++ b/vis/makefile @@ -8,3 +8,6 @@ test: clean: rm -rf *.pyc __pycache__ + +dist: + zip python.zip *.py xively/*.py diff --git a/vis/visual.py b/vis/visual.py index f2deed6..737e2d9 100644 --- a/vis/visual.py +++ b/vis/visual.py @@ -17,7 +17,7 @@ else: class Visual: FRAMES_PER_SEC = 60 # hz - RATES_PER_SEC = 10 # hz + RATES_PER_SEC = 1 # hz def __init__(self, config, device, logger): def get_objects(names): @@ -61,14 +61,14 @@ class Visual: if act.get_active(): self.status(self.device.connect()) else: - self.status(self.device.disconnect()) + self.device.disconnect() return True def on_xively(self, act): if act.get_active(): self.status(self.logger.connect()) else: - self.status(self.logger.disconnect()) + self.logger.disconnect() return True def on_flush(self, act): @@ -224,7 +224,8 @@ class Visual: # Private methods def status(self, msg): + status = self.builder.get_object("status") + status.pop(context_id=0) if not msg: return - status = self.builder.get_object("status") status.push(text=('Error: ' + msg), context_id=0) diff --git a/yue/main.cpp b/yue/main.cpp index 218b27a..150be36 100644 --- a/yue/main.cpp +++ b/yue/main.cpp @@ -87,7 +87,13 @@ MMA8451Q accSensor(PTE25, PTE24, MMA8451_I2C_ADDRESS); MAG3110 magSensor(PTE25, PTE24); TSISensor tchSensor; AnalogIn lgtSensor(PTE22); -AnalogIn a2dSensor(A0); + +AnalogIn a2dSensor0(A0); +AnalogIn a2dSensor1(A1); +AnalogIn a2dSensor2(A2); +AnalogIn a2dSensor3(A3); +AnalogIn a2dSensor4(A4); +AnalogIn a2dSensor5(A5); AnalogOut a2dOutput(PTE30); @@ -220,7 +226,7 @@ void clock1_interrupt(void){ static int ledCnt; // Write A2D output sine wave - a2dOutput.write(sin(a2dCnt * TIME_ACCURACY * (2*PI) * 0.1)); + //a2dOutput.write(sin(a2dCnt * TIME_ACCURACY * (2*PI) * 0.1)); // Send data through Serial if (accEnable && accCnt >= (int)(accTmr/TIME_ACCURACY+0.5)){ @@ -373,12 +379,12 @@ void sendTchInfo(void){ void sendA2dInfo(void){ float a2dData[6]; - a2dData[0] = a2dSensor.read(); - a2dData[1] = 0; - a2dData[2] = 0; - a2dData[3] = 0; - a2dData[4] = 0; - a2dData[5] = 0; + a2dData[0] = a2dSensor0.read(); + a2dData[1] = a2dSensor1.read(); + a2dData[2] = a2dSensor2.read(); + a2dData[3] = a2dSensor3.read(); + a2dData[4] = a2dSensor4.read(); + a2dData[5] = a2dSensor5.read(); int len = packToFrame(txFrame, SNS_A2D, TYP_F32, 6, a2dData); printStr("[A2D] data=%2.2f %2.2f %2.2f %2.2f %2.2f %2.2f\r\n", diff --git a/yue/makefile b/yue/makefile index 19f0bbb..86b2f59 100644 --- a/yue/makefile +++ b/yue/makefile @@ -6,5 +6,8 @@ LDFLAGS = -lm default: info install +dist: + zip mbed.zip makefile ../common.mk *.{c,cpp,h} */*.{cpp,.h} + -include ../config.mk -include ../common.mk