Skip to content

Commit 9e86ede

Browse files
committed
update
1 parent 3e21681 commit 9e86ede

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+5942
-0
lines changed

Traffic counting System based on OpenCV and python/cars.xml

+3,654
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import cv2
4+
print(cv2.__version__)
5+
6+
cascade_src = 'cars.xml'
7+
video_src = 'dataset/video2.avi'
8+
#video_src = 'dataset/video2.avi'
9+
10+
cap = cv2.VideoCapture(video_src)
11+
car_cascade = cv2.CascadeClassifier(cascade_src)
12+
13+
while True:
14+
ret, img = cap.read()
15+
if (type(img) == type(None)):
16+
break
17+
18+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
19+
20+
cars = car_cascade.detectMultiScale(gray, 1.1, 1)
21+
22+
for (x,y,w,h) in cars:
23+
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
24+
25+
cv2.imshow('video', img)
26+
print "Found "+str(len(cars))+" car(s)"
27+
b=str(len(cars))
28+
a= float(b)
29+
if a>=5:
30+
print ("more traffic")
31+
else:
32+
print ("no traffic")
33+
if cv2.waitKey(33) == 27:
34+
break
35+
36+
cv2.destroyAllWindows()

_Learn/Ansmann-lithIon.PDF

260 KB
Binary file not shown.
65.3 KB
Loading
152 KB
Loading

bme680-raspberry/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 twartzek
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

bme680-raspberry/README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# BME680 interface with Raspberry Pi
2+
## Introduction
3+
4+
This program serves as a working example to read the measurements from a BME680 via the raspberry pi. Actually I wanted to read out the sensor via python, but as the driver provided by Bosch is a C file, I created this small C program to communicate to the sensor. The compile program can then be called from python.
5+
6+
7+
## Usage guide
8+
### Compiling the program
9+
Download the driver for the [BME680](https://github.com/BoschSensortec/BME680_driver) from github.
10+
Put the file from this repository into the same directory.
11+
12+
As this program uses i2c-dev, check you can use `i2cdetect -y 1` to verify if your I²C communication works via bus 1. You might need to adapt the code according your I²C Bus in the line `g_i2cFid = open("/dev/i2c-1", O_RDWR);` to `g_i2cFid = open("/dev/i2c-0", O_RDWR);` if bus 0 is used. Also check the I²C address of your sensor. The provided code uses `BME680_I2C_ADDR_SECONDARY` (0x77) as the address.
13+
14+
15+
After this adaption you can compile the program with
16+
`gcc bme680_main.c bme680.c -o bme680`
17+
18+
19+
### Command line interface
20+
The program can be called with up to three parameters. However, be aware that no special input check is implemented.
21+
1. `./bme680`: This will print three measurement results with the time of the measurement on the standard output stream. The delay between each measurement is 3 seconds.
22+
```shell
23+
pi@raspberrypi:~/myproject/sensors/bme680 $ ./bme680
24+
**** BME680 start measurements ****
25+
2017-10-07 19:48:18 T: 19.51 degC, P: 1013.08 hPa, H: 69.61 %rH , G: 3954 ohms
26+
2017-10-07 19:48:21 T: 19.53 degC, P: 1013.06 hPa, H: 69.43 %rH , G: 9471 ohms
27+
2017-10-07 19:48:24 T: 19.54 degC, P: 1013.04 hPa, H: 69.28 %rH , G: 16839 ohms
28+
**** Measurement finished ****
29+
pi@raspberrypi:~/myproject/sensors/bme680 $
30+
```
31+
2. `./bme680 5 2`: The first parameter defines the delay between each measurement. The second parameter defines the number of measurements before the program is terminated.
32+
```shell
33+
pi@raspberrypi:~/myproject/sensors/bme680 $ ./bme680 5 2
34+
**** BME680 start measurements ****
35+
2017-10-07 20:02:41 T: 19.51 degC, P: 1012.86 hPa, H: 69.87 %rH , G: 26970 ohms
36+
2017-10-07 20:02:46 T: 19.52 degC, P: 1012.84 hPa, H: 69.71 %rH , G: 44102 ohms
37+
**** Measurement finished ****
38+
pi@raspberrypi:~/myproject/sensors/bme680 $
39+
```
40+
3. `./bme680 5 2 output.txt`: You can also provide a third argument, which will be used to write the measurements into a file.
41+
42+
### Usage within Python
43+
If you want to use this program in python, you can simply call it with `check_output`.
44+
45+
```python
46+
from subprocess import check_output
47+
out = check_output(["bme680", str(measDelay), str(nMeas)])
48+
```
49+
If any problems with concurrent I²C bus usages occur, you can check if the file *~bme680i2c.lock* exists, as this is created at the start of the program and deleted shortly before termination.
50+
51+
52+
53+
54+
55+

bme680-raspberry/bme680_main.c

+283
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
/******************************
2+
* Copyright (C) 2017 by Tobias Wartzek
3+
* @file bme680_main.c
4+
* @date 07.10.2017
5+
* @version 1.0
6+
* @brief Interface to BME680 from Raspberry Pi
7+
*
8+
*
9+
* Read out temperature, pressure, humidity and gas sensor ohmic values
10+
* via I²C and Raspberry Pi.
11+
*
12+
* History
13+
* Version Date Detail
14+
* 1.0 07.10.2017 Initial creation
15+
*
16+
******************************/
17+
18+
19+
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
#include <time.h>
23+
#include <linux/i2c-dev.h>
24+
#include <fcntl.h>
25+
#include <string.h>
26+
#include <sys/ioctl.h>
27+
#include <sys/types.h>
28+
#include <sys/stat.h>
29+
#include <unistd.h>
30+
#include "bme680.h"
31+
32+
#define DESTZONE "TZ=Europe/Stockholm" // Our destination time zone
33+
34+
35+
36+
// I2C Linux device handle
37+
int g_i2cFid;
38+
39+
// open the Linux device
40+
void i2cOpen()
41+
{
42+
g_i2cFid = open("/dev/i2c-1", O_RDWR);
43+
if (g_i2cFid < 0) {
44+
perror("i2cOpen");
45+
exit(1);
46+
}
47+
}
48+
49+
// close the Linux device
50+
void i2cClose()
51+
{
52+
close(g_i2cFid);
53+
}
54+
55+
// set the I2C slave address for all subsequent I2C device transfers
56+
void i2cSetAddress(int address)
57+
{
58+
if (ioctl(g_i2cFid, I2C_SLAVE, address) < 0) {
59+
perror("i2cSetAddress");
60+
exit(1);
61+
}
62+
}
63+
64+
65+
66+
void user_delay_ms(uint32_t period)
67+
{
68+
69+
sleep(period/1000);
70+
71+
72+
73+
}
74+
75+
int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
76+
{
77+
int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
78+
79+
uint8_t reg[1];
80+
reg[0]=reg_addr;
81+
82+
if (write(g_i2cFid, reg, 1) != 1) {
83+
perror("user_i2c_read_reg");
84+
rslt = 1;
85+
}
86+
if (read(g_i2cFid, reg_data, len) != len) {
87+
perror("user_i2c_read_data");
88+
rslt = 1;
89+
}
90+
91+
return rslt;
92+
}
93+
94+
int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
95+
{
96+
int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
97+
98+
99+
uint8_t reg[16];
100+
reg[0]=reg_addr;
101+
102+
for (int i=1; i<len+1; i++)
103+
reg[i] = reg_data[i-1];
104+
105+
if (write(g_i2cFid, reg, len+1) != len+1) {
106+
perror("user_i2c_write");
107+
rslt = 1;
108+
exit(1);
109+
}
110+
111+
return rslt;
112+
}
113+
114+
115+
void write2file(char *outputFile, struct tm tm, struct bme680_field_data data)
116+
{
117+
// Write measurement to output file if specified.
118+
if(outputFile != NULL)
119+
{
120+
FILE *f = fopen(outputFile, "a");
121+
if (f == NULL)
122+
{
123+
printf("Error opening file!\n");
124+
//exit(1);
125+
}
126+
else
127+
{
128+
fprintf(f,"%d-%02d-%02d %02d:%02d:%02d ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
129+
fprintf(f,"T: %.2f degC, P: %.2f hPa, H: %.2f %%rH", data.temperature / 100.0f,
130+
data.pressure / 100.0f, data.humidity / 1000.0f );
131+
fprintf(f,", G: %d Ohms", data.gas_resistance);
132+
fprintf(f,"\r\n");
133+
fclose(f);
134+
}
135+
136+
}
137+
}
138+
139+
int main(int argc, char *argv[] )
140+
{
141+
// create lock file first
142+
FILE *f = fopen("~bme680i2c.lock", "w");
143+
if (f == NULL)
144+
{
145+
printf("Error opening file!\n");
146+
exit(1);
147+
}
148+
fprintf(f,"I2C locked by BME680 readout. \r\n");
149+
fclose(f);
150+
151+
152+
int delay = 3;
153+
int nMeas = 3;
154+
char *outputFile = NULL;
155+
156+
// Input argument parser
157+
if( argc == 2 ) {
158+
delay = strtol(argv[1], NULL, 10);
159+
}
160+
else if( argc == 3 ) {
161+
delay = strtol(argv[1], NULL, 10);
162+
nMeas = strtol(argv[2], NULL, 10);
163+
}
164+
else if( argc == 4 ) {
165+
delay = strtol(argv[1], NULL, 10);
166+
nMeas = strtol(argv[2], NULL, 10);
167+
outputFile = argv[3];
168+
}
169+
else {
170+
171+
}
172+
173+
174+
printf("**** BME680 start measurements ****\n");
175+
176+
time_t t = time(NULL);
177+
putenv(DESTZONE); // Switch to destination time zone
178+
179+
180+
// open Linux I2C device
181+
i2cOpen();
182+
183+
// set address of the BME680
184+
i2cSetAddress(BME680_I2C_ADDR_SECONDARY);
185+
186+
// init device
187+
struct bme680_dev gas_sensor;
188+
189+
gas_sensor.dev_id = BME680_I2C_ADDR_SECONDARY;
190+
gas_sensor.intf = BME680_I2C_INTF;
191+
gas_sensor.read = user_i2c_read;
192+
gas_sensor.write = user_i2c_write;
193+
gas_sensor.delay_ms = user_delay_ms;
194+
195+
int8_t rslt = BME680_OK;
196+
rslt = bme680_init(&gas_sensor);
197+
198+
uint8_t set_required_settings;
199+
200+
/* Set the temperature, pressure and humidity settings */
201+
gas_sensor.tph_sett.os_hum = BME680_OS_2X;
202+
gas_sensor.tph_sett.os_pres = BME680_OS_4X;
203+
gas_sensor.tph_sett.os_temp = BME680_OS_8X;
204+
gas_sensor.tph_sett.filter = BME680_FILTER_SIZE_3;
205+
206+
/* Set the remaining gas sensor settings and link the heating profile */
207+
gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
208+
/* Create a ramp heat waveform in 3 steps */
209+
gas_sensor.gas_sett.heatr_temp = 320; /* degree Celsius */
210+
gas_sensor.gas_sett.heatr_dur = 150; /* milliseconds */
211+
212+
/* Select the power mode */
213+
/* Must be set before writing the sensor configuration */
214+
gas_sensor.power_mode = BME680_FORCED_MODE;
215+
216+
/* Set the required sensor settings needed */
217+
set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL
218+
| BME680_GAS_SENSOR_SEL;
219+
220+
/* Set the desired sensor configuration */
221+
rslt = bme680_set_sensor_settings(set_required_settings,&gas_sensor);
222+
223+
/* Set the power mode */
224+
rslt = bme680_set_sensor_mode(&gas_sensor);
225+
226+
227+
228+
/* Get the total measurement duration so as to sleep or wait till the
229+
* measurement is complete */
230+
uint16_t meas_period;
231+
bme680_get_profile_dur(&meas_period, &gas_sensor);
232+
user_delay_ms(meas_period + delay*1000); /* Delay till the measurement is ready */
233+
234+
235+
struct bme680_field_data data;
236+
237+
struct tm tm = *localtime(&t);
238+
239+
int i=0;
240+
int backupCounter = 0;
241+
242+
while(i<nMeas && backupCounter < nMeas+5) {
243+
244+
// Get sensor data
245+
rslt = bme680_get_sensor_data(&data, &gas_sensor);
246+
247+
// Avoid using measurements from an unstable heating setup
248+
if(data.status & BME680_HEAT_STAB_MSK)
249+
{
250+
t = time(NULL);
251+
tm = *localtime(&t);
252+
printf("%d-%02d-%02d %02d:%02d:%02d ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
253+
printf("T: %.2f degC, P: %.2f hPa, H: %.2f %%rH", data.temperature / 100.0f,
254+
data.pressure / 100.0f, data.humidity / 1000.0f );
255+
printf(", G: %d Ohms", data.gas_resistance);
256+
printf("\r\n");
257+
write2file(outputFile, tm, data);
258+
i++;
259+
}
260+
261+
// Trigger a meausurement
262+
rslt = bme680_set_sensor_mode(&gas_sensor); /* Trigger a measurement */
263+
264+
// Wait for a measurement to complete
265+
user_delay_ms(meas_period + delay*1000); /* Wait for the measurement to complete */
266+
267+
backupCounter++;
268+
}
269+
270+
271+
printf("**** Measurement finished ****\n");
272+
273+
274+
275+
// close Linux I2C device
276+
i2cClose();
277+
278+
// delete lock file
279+
remove("~bme680i2c.lock");
280+
281+
return 0;
282+
}
283+

0 commit comments

Comments
 (0)