Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boards calibration #67

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b6c7a82
Calibration: countdown timer (#58)
marcomicera Nov 24, 2019
6f7e8e4
Calibration: RSSI average (#58)
marcomicera Nov 25, 2019
d41cd46
Calibration: using custom 1-meter-distance RSSI values (#58)
marcomicera Nov 25, 2019
b904003
Calibration: JSON dedicated object + README instructions (#58)
marcomicera Nov 26, 2019
f154795
Calibration: average computation fix (#58)
marcomicera Nov 26, 2019
4d568ab
Calibration: placement duration parameter description fix (#58)
marcomicera Nov 26, 2019
07591df
Calibration: uniform "placement" terminology (#58)
marcomicera Nov 26, 2019
cc97777
Async calibration trial (#58)
marcomicera Nov 26, 2019
58303f3
Calibration should now work. It still has to be tested with
simonasaitta Nov 26, 2019
141376b
Now you should not have to wait anymore between one board calibration…
simonasaitta Nov 26, 2019
0a4484a
Missing check on bytes sent from board
simonasaitta Nov 27, 2019
21b0c70
Seems to have fixed issue with board send but needs further testing.
simonasaitta Nov 27, 2019
b043c24
Resolved some instances of board that would get it stuck. Now core di…
simonasaitta Nov 27, 2019
23c7c08
Synchronized batch sending between boards
simonasaitta Nov 27, 2019
d75e540
Uniform board TAG w/ MAC last two digits. Closes #62
marcomicera Nov 28, 2019
93d45b4
Removed verbose logging (calib. batches sent by other devices)
marcomicera Nov 28, 2019
3a6ea5c
Calibration: comments + static calibrated boards counter
marcomicera Nov 28, 2019
365ab18
Calibration: logging announced distances once per estimation
marcomicera Nov 28, 2019
9fd7c53
Some refactoring and comments
simonasaitta Nov 28, 2019
4fb7d8c
point is discarded if not in room
simonasaitta Nov 28, 2019
0854975
Merge branch 'master' into calibration
marcomicera Dec 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions board/followifier/main/components/flusher.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,33 @@ void flush(void) {

// Flushing the message buffer
ESP_LOGI(BOARD_TAG, "Sending batch");
ESP_ERROR_CHECK_JUMP_LABEL(send(tcp_socket, (char *) buffer, batch_length + sizeof("\n\r\n\r"), 0) >= 0,
"Error while sending batch: discarding local packets, re-enabling sniffing mode...",
closing_socket);
ESP_LOGI(BOARD_TAG, "Sending delimiter...");
size_t to_send = batch_length + sizeof("\n\r\n\r");
size_t sent = 0;
do {
if (sent > 0)
ESP_LOGI(BOARD_TAG, "Resending missing bytes");
int sent_now = send(tcp_socket, (char *) buffer + sent, to_send - sent, SO_LINGER);
ESP_ERROR_CHECK_JUMP_LABEL(sent_now >= 0,
"Error while sending batch: discarding local packets, re-enabling sniffing mode...",
closing_socket);
sent += sent_now;
ESP_LOGI(BOARD_TAG, "%d", sent);
} while (sent < to_send);

// Skipping until here in case connection towards the server was unsuccessful
closing_socket:

// Closing socket
ESP_LOGI(BOARD_TAG, "Shutting down socket towards %s:%d...", SERVER_ADDRESS, SERVER_PORT);
shutdown(tcp_socket, SHUT_RDWR);
shutdown(tcp_socket, SHUT_WR);
for (int i = 0; i < 5; i++) {
//5 was decided to avoid looping
size_t res = read(tcp_socket, buffer, 4000);
if (!res)
break;
ESP_LOGI(BOARD_TAG, "looping");
}

close(tcp_socket);
ESP_LOGI(BOARD_TAG, "...socket towards %s:%d closed.", SERVER_ADDRESS, SERVER_PORT);

Expand Down
11 changes: 9 additions & 2 deletions board/followifier/main/components/sniffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,14 @@ void *sniffer_timer(void *args) {
#endif

ESP_LOGI(BOARD_TAG, "Flush timer started.");
vTaskDelay(portTICK_PERIOD_MS * FLUSH_RATE_IN_SECONDS * 10); // in deci-seconds (0.1 seconds)
//calculate seconds needed to reach 00 seconds
time_t now;
struct tm *tm;
now = time(0);
tm = localtime (&now);
int seconds = 60 - tm->tm_sec;
ESP_LOGI(BOARD_TAG, "Flush will be in %d seconds", seconds);
vTaskDelay(portTICK_PERIOD_MS * seconds * 10); // in deci-seconds (0.1 seconds)

#ifdef DEBUG_ONE_DEVICE_TRACKING
ESP_LOGI(BOARD_TAG, "Measurement is over: please re-adjust the distance between the testing device and this board "
Expand All @@ -311,7 +318,7 @@ void *sniffer_timer(void *args) {
max_rrsi_in_measure_period = INT_MIN;
#endif

ESP_LOGI(BOARD_TAG, "Flush timer expired (%d seconds): time to flush the batch.", FLUSH_RATE_IN_SECONDS);
ESP_LOGI(BOARD_TAG, "Flush timer expired (%d seconds): time to flush the batch.", seconds);
prepare_to_flush(true);
return NULL;
}
Expand Down
5 changes: 0 additions & 5 deletions board/followifier/main/components/sniffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ extern signed max_rrsi_in_measure_period;

#endif

/**
* Board flush rate in seconds.
*/
#define FLUSH_RATE_IN_SECONDS (60)

/**
* @brief Supported Sniffer Interface
*
Expand Down
2 changes: 2 additions & 0 deletions board/followifier/main/components/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ void init_wifi(void) {
}

esp_err_t start_wifi(void) {
wifi_stopped = false;
return esp_wifi_start();
}

esp_err_t stop_wifi(void) {
wifi_stopped = true;
return esp_wifi_stop();
}
5 changes: 5 additions & 0 deletions board/followifier/main/components/wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ esp_err_t start_wifi();
*/
esp_err_t stop_wifi();


/**
* Check if wifi was stopped voluntary
*/
static bool wifi_stopped = true;
#endif //FOLLOWIFIER_WIFI_H
8 changes: 8 additions & 0 deletions board/followifier/main/followifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ esp_err_t board_event_handler(void *ctx, system_event_t *event) {
case SYSTEM_EVENT_STA_DISCONNECTED: // Board got disconnected from AP

ESP_LOGI(BOARD_TAG, "Board got disconnected from the \"%s\" Wi-Fi network.", WIFI_SSID);

// TODO: Sometimes it would get stuck here for me. I've tried to fix it but I wasn't able to reproduce it anymore.
if(!wifi_stopped) {

// Wifi wasn't voluntary stopped
init_sniffer();
start_sniffer();
}
break;

// case SYSTEM_EVENT_STA_STOP: // Board stops
Expand Down
2 changes: 1 addition & 1 deletion core/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ add_executable(${PROJECT_NAME} src/receiver.cpp src/receiver.h ${PROTOBUF_MODELS
src/main.cpp
src/connection.h src/connection.cpp
src/server.cpp src/server.h
src/database.cpp src/database.h src/board.h src/point.h src/settings.cpp src/settings.h src/board.cpp src/point.cpp src/statistics.cpp src/statistics.h)
src/database.cpp src/database.h src/board.h src/point.h src/settings.cpp src/settings.h src/board.cpp src/point.cpp src/statistics.cpp src/statistics.h src/calibration.cpp src/calibration.h src/room.cpp src/room.h)
target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})
if (Boost_FOUND)
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
Expand Down
20 changes: 18 additions & 2 deletions core/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and [`mongocxx` drivers](http://mongocxx.org/mongocxx-v3/installation/)
```bash
$ sudo service mongod start
```
1. Create a configuration file `config.json` having the following format:
1. Create a configuration file `config.json` having the following format: <!-- TODO add missing fields -->
```json
{
"port": 12345,
Expand All @@ -33,9 +33,25 @@ and [`mongocxx` drivers](http://mongocxx.org/mongocxx-v3/installation/)
{"x": 0, "y": 90},
{"x": 90, "y": 0},
{"x": 90, "y": 90}
]
],
"calibration": {
"calibration_device": "cc:61:e5:13:1a:d1",
"min_num_calibration_messages": 2,
"placement_duration_in_seconds": 15
}
}
```
- The `calibration` object is completely optional, and refers to the initial boards calibration phase during
the user is asked to place a mobile device at 1 meter from all boards, one at the time.\
If missing, calibration will not be performed.
- The mobile calibration device that has to be carried around is `calibration_device`.\
If missing, calibration will not be performed.
- `min_num_calibration_messages` represents the minimum number of messages that the calibration device needs
to send in a batch in order for the calibration to make sense.\
If missing, a default value will be used.
- `placement_duration_in_seconds` represents the number of seconds that the user has at her/his disposal
in order to place the mobile device at one meter for boards, one at the time.
If missing, a default value will be used.
1. Optionally, free the `core` server port:
```bash
lsof -i tcp:12345 | grep LISTEN | awk '{print $2}' | xargs kill
Expand Down
3 changes: 2 additions & 1 deletion core/server/src/board.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "board.h"
#include <regex>
#include <boost/algorithm/string/predicate.hpp>

bool Board::operator==(const Board &other) const{
return this->coordinates == other.coordinates &&
this->mac == other.mac;
boost::iequals(this->mac, other.mac); // case-insensitive comparison
}

std::string Board::getMac() {
Expand Down
6 changes: 0 additions & 6 deletions core/server/src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ class Board {

bool operator==(const Board &other) const;

struct BoardHasher {
size_t operator()(const Board &b) const {
return std::hash<std::string>()(b.mac);
}
};

private:

std::string mac; //board mac
Expand Down
30 changes: 30 additions & 0 deletions core/server/src/calibration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "calibration.h"

std::string calibration::board_to_calibrate;
long int calibration::starting_timestamp;
short calibration::board_counter = 0;
bool calibration::board_has_sent_calibration_batch;


void calibration::wait_placement(const std::string &board_mac) {

/* How many seconds are left for the user so s/he can place the board at 1 meter distance */
int board_placements_seconds_left = Settings::configuration.calibration_placement_duration_in_seconds.value();

std::cout << "Please place device " << Settings::configuration.calibration_device_mac_address.value()
<< " at 1 meter distance from board " << board_mac << " (" << (++calibration::board_counter) << "/"
<< Settings::get_num_boards() << ")." << std::endl;

/* `INITIALIZATION_BOARD_PLACEMENT_WAITING_TIME` seconds countdown starts now */
while (board_placements_seconds_left > 0) {
std::cout << "\r" << board_placements_seconds_left << " second"
<< (board_placements_seconds_left > 1 ? "s" : "") << " left... " << std::flush;
sleep(1);
--board_placements_seconds_left;
}
std::cout << "\rReady to calibrate board " << board_mac << ". Please do not move the device any further."
<< std::endl;
/* The timestamp corresponding to the instant the device has been placed is stored so that it can be used
* to discard messages before that instant*/
starting_timestamp = std::time(nullptr);
}
39 changes: 39 additions & 0 deletions core/server/src/calibration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Created by simona on 26/11/19.
//

#ifndef CORE_CALIBRATION_H
#define CORE_CALIBRATION_H


#include <string>
#include "settings.h"
#include <ctime>

class calibration {

protected:

/**
* How many boards have been calibrated so far.
*/
static short board_counter;

public:

static std::string board_to_calibrate;

static long int starting_timestamp;

static bool board_has_sent_calibration_batch;

/**
* Waits for the user to place the specified board at 1 meter distance from the server.
*
* @param board_mac MAC address of the board to be placed.
*/
static void wait_placement(const std::string &board_mac);

};

#endif //CORE_CALIBRATION_H
Loading