Руслан Бредун / Mbed 2 deprecated stm32-sensor-base2

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Committer:
ommpy
Date:
Mon Jul 06 17:18:59 2020 +0530
Revision:
0:d383e2dee0f7
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ommpy 0:d383e2dee0f7 1 # stm32-sensor-base
ommpy 0:d383e2dee0f7 2 STM32-based sensor node that can communicate with AS5045 encoders, JSN-SR04 ultrasonic sensors and transmit data over RS485 interface.Designed to work with ROS (http://wiki.ros.org/Documentation), it has some benefits over default rosserial package (http://wiki.ros.org/rosserial) in large data transmission reliability achieved due to using RS485 interface. Another important note : it can be easily extended in order to add support of other essential sensors. Simplicity of the project is achieved due to using the Mbed OS project that brings real-time features and many other important things
ommpy 0:d383e2dee0f7 3
ommpy 0:d383e2dee0f7 4 ## Table of contents
ommpy 0:d383e2dee0f7 5 * [Information](#info)
ommpy 0:d383e2dee0f7 6 * [Structure of the package](#structure)
ommpy 0:d383e2dee0f7 7 * [Project folders description](#folders)
ommpy 0:d383e2dee0f7 8 * [Files explained](#files)
ommpy 0:d383e2dee0f7 9 * [RS485 Library](#rs485)
ommpy 0:d383e2dee0f7 10 * [Class description](#class-description)
ommpy 0:d383e2dee0f7 11 * [List of methods](#methods)
ommpy 0:d383e2dee0f7 12 * [List of fields](#fields)
ommpy 0:d383e2dee0f7 13 * [JSN-SR04 Library](#jsn-sr04)
ommpy 0:d383e2dee0f7 14 * [Class description](#class-description-1)
ommpy 0:d383e2dee0f7 15 * [List of methods](#methods-1)
ommpy 0:d383e2dee0f7 16 * [List of fields](#fields-1)
ommpy 0:d383e2dee0f7 17 * [AS5045 Library](#as5045)
ommpy 0:d383e2dee0f7 18 * [Class description](#class-description-2)
ommpy 0:d383e2dee0f7 19 * [List of methods](#methods-2)
ommpy 0:d383e2dee0f7 20 * [List of fields](#fields-2)
ommpy 0:d383e2dee0f7 21 * [E18_D80NK Library](#e18_d80nk)
ommpy 0:d383e2dee0f7 22 * [Class description](#class-description-3)
ommpy 0:d383e2dee0f7 23 * [List of methods](#methods-3)
ommpy 0:d383e2dee0f7 24 * [List of fields](#fields-3)
ommpy 0:d383e2dee0f7 25 * [Using](#using)
ommpy 0:d383e2dee0f7 26
ommpy 0:d383e2dee0f7 27 ## Info
ommpy 0:d383e2dee0f7 28 * MCU : STM32F103
ommpy 0:d383e2dee0f7 29 * OS : Mbed OS (https://os.mbed.com/mbed-os/)
ommpy 0:d383e2dee0f7 30
ommpy 0:d383e2dee0f7 31 ## Structure
ommpy 0:d383e2dee0f7 32 ```
ommpy 0:d383e2dee0f7 33 ├── AS5045
ommpy 0:d383e2dee0f7 34 │   ├── AS5045.cpp
ommpy 0:d383e2dee0f7 35 │   └── AS5045.h
ommpy 0:d383e2dee0f7 36 ├── doc
ommpy 0:d383e2dee0f7 37 │   └── images
ommpy 0:d383e2dee0f7 38 ├── E18_D80NK
ommpy 0:d383e2dee0f7 39 │   ├── E18_D80NK.cpp
ommpy 0:d383e2dee0f7 40 │   └── E18_D80NK.h
ommpy 0:d383e2dee0f7 41 ├── JSN_SR04
ommpy 0:d383e2dee0f7 42 │   ├── JSN_SR04.cpp
ommpy 0:d383e2dee0f7 43 │   └── JSN_SR04.h
ommpy 0:d383e2dee0f7 44 ├── mbed
ommpy 0:d383e2dee0f7 45 ├── ros_lib_melodic
ommpy 0:d383e2dee0f7 46 ├── RS485
ommpy 0:d383e2dee0f7 47 │   ├── mbed
ommpy 0:d383e2dee0f7 48 │   ├── mbed.bld
ommpy 0:d383e2dee0f7 49 │   ├── RS485.cpp
ommpy 0:d383e2dee0f7 50 │   └── RS485.h
ommpy 0:d383e2dee0f7 51 ├── AS5045.lib
ommpy 0:d383e2dee0f7 52 ├── CHANGELOG.md
ommpy 0:d383e2dee0f7 53 ├── global.h
ommpy 0:d383e2dee0f7 54 ├── JSN_SR04.lib
ommpy 0:d383e2dee0f7 55 ├── main.cpp
ommpy 0:d383e2dee0f7 56 ├── mbed.bld
ommpy 0:d383e2dee0f7 57 ├── mbed_config.h
ommpy 0:d383e2dee0f7 58 ├── README.md
ommpy 0:d383e2dee0f7 59 ├── ros_lib_melodic.lib
ommpy 0:d383e2dee0f7 60 ├── RS485Hardware.h
ommpy 0:d383e2dee0f7 61 └── RS485.lib
ommpy 0:d383e2dee0f7 62 ```
ommpy 0:d383e2dee0f7 63
ommpy 0:d383e2dee0f7 64 ### Folders
ommpy 0:d383e2dee0f7 65 * AS5045 : library to work with AS5045 encoders
ommpy 0:d383e2dee0f7 66 * E18_D80NK : allows the node to communicate with the E18-D80NK sensor
ommpy 0:d383e2dee0f7 67 * JSN_SR04 : integration with JSN-SR04 ultrasonic sensors
ommpy 0:d383e2dee0f7 68 * RS485 : data transmission library that initiate data R/W over RS485 serial interface
ommpy 0:d383e2dee0f7 69 * mbed : Mbed OS library
ommpy 0:d383e2dee0f7 70 * ros_lib_melodic : ROS library
ommpy 0:d383e2dee0f7 71 * doc : documentation data
ommpy 0:d383e2dee0f7 72 * images : images to use in the documentation
ommpy 0:d383e2dee0f7 73
ommpy 0:d383e2dee0f7 74 ### Files
ommpy 0:d383e2dee0f7 75 * AS5045.cpp : AS5045 methods implementation
ommpy 0:d383e2dee0f7 76 * AS5045.h : AS5045 header that implements AS5045 class (retrieve data from the encoder)
ommpy 0:d383e2dee0f7 77 * E18_D80NK.h : header which contains a code to communicate with the E18-D80NK sensor
ommpy 0:d383e2dee0f7 78 * E18_D80NK.cpp : methods implementation for the E18_D80NK class
ommpy 0:d383e2dee0f7 79 * JSN_SR04.cpp : JSN-SR04 library methods implementation
ommpy 0:d383e2dee0f7 80 * JSN_SR04.h : header with a class which holds data exchange between STM32 and JSN-SR04 ultrasonic sensor
ommpy 0:d383e2dee0f7 81 * RS485.cpp : RS485 methods implementation
ommpy 0:d383e2dee0f7 82 * RS485.h : data transmission/receiving through RS485
ommpy 0:d383e2dee0f7 83 * AS5045.lib : the encoder library import file
ommpy 0:d383e2dee0f7 84 * global.h : pin settings on an STM32
ommpy 0:d383e2dee0f7 85 * JSN_SR04.lib : the ultrasonic sensor library import file
ommpy 0:d383e2dee0f7 86 * main.cpp : firmware main execution file
ommpy 0:d383e2dee0f7 87 * mbed.bld : the official Mbed 2 C/C++ SDK link
ommpy 0:d383e2dee0f7 88 * mbed_config.h : STM32 Mbed OS configuration file
ommpy 0:d383e2dee0f7 89 * README.md : documentation (this file)
ommpy 0:d383e2dee0f7 90 * ros_lib_melodic.lib : ROS library import
ommpy 0:d383e2dee0f7 91 * RS485Hardware.h : ???
ommpy 0:d383e2dee0f7 92 * RS485.lib : RS485 interface library import
ommpy 0:d383e2dee0f7 93
ommpy 0:d383e2dee0f7 94 ## RS485
ommpy 0:d383e2dee0f7 95 RS485 library uses the most versatile communication standard in the standard series defined by the EIA of the same name to transfer data through it. Library is designed to send data byte-by-byte using predefined pins.
ommpy 0:d383e2dee0f7 96
ommpy 0:d383e2dee0f7 97 ![RS485](doc/images/RS485.png?raw=true "RS485 Class Diagram")
ommpy 0:d383e2dee0f7 98
ommpy 0:d383e2dee0f7 99 ### Class description
ommpy 0:d383e2dee0f7 100 The class establishes input and output pins through Serial base class. Once the pins are ready, the base class handles all data transmissions and the RS485 class is an interface. Data transmission can be performed using \_getc() (receive byte) and \_putc() (send byte).
ommpy 0:d383e2dee0f7 101
ommpy 0:d383e2dee0f7 102 #### Methods
ommpy 0:d383e2dee0f7 103 * RS485() : sets pins for input/output and CS pin (using base class constructor)
ommpy 0:d383e2dee0f7 104 * \_putc() : sends one byte of data (8 bits) through RS485 interface
ommpy 0:d383e2dee0f7 105 * \_getc() : receives one byte of data through RS485 interface
ommpy 0:d383e2dee0f7 106
ommpy 0:d383e2dee0f7 107 #### Fields
ommpy 0:d383e2dee0f7 108 * m_modePin : a pin which sets the mode of transmission
ommpy 0:d383e2dee0f7 109
ommpy 0:d383e2dee0f7 110 ## JSN-SR04
ommpy 0:d383e2dee0f7 111 JSN_SR04 uses ultrasonic sensors connected to the internal timer to get distance to an obstacle. Class JSN_SR04 implements basic functionality to measure distance in mm and cm, set boundaries and start/stop the measurement, check if the new data was obtained.
ommpy 0:d383e2dee0f7 112
ommpy 0:d383e2dee0f7 113 ![JSN-SR04](doc/images/JSN_SR04.png?raw=true "JSN-SR04 Class Diagram")
ommpy 0:d383e2dee0f7 114
ommpy 0:d383e2dee0f7 115 ### Class description
ommpy 0:d383e2dee0f7 116 There is a timer event inside of the class that ( triggerTimeout) trigger the sensor to start receiving data from it once in a period. After triggering, sensor sends a respond (external interrupt trigger is set HIGH) that corresponds to distance to an obstacle (formula for converting is on the 2 page of the datasheet). Measured value is saved inside of the class and can be accessed through getDistance_cm() and getDistance_mm() methods. Freshness of the data can be checked through isNewDataReady().
ommpy 0:d383e2dee0f7 117
ommpy 0:d383e2dee0f7 118 #### Methods
ommpy 0:d383e2dee0f7 119 * JSN_SR04() : assign pins to be a digital output or interrupt input (measurement works on an interrupt mechanism)
ommpy 0:d383e2dee0f7 120 * startMeasurement() : start the measurement of distance to the next obstacle and attach timer events (to automatically send trigger signal and start measuring time when interrupt signal is high (time in corresponds to distance to an obstacle))
ommpy 0:d383e2dee0f7 121 * getDistance_mm() : get measured distance to the obstacle in mm
ommpy 0:d383e2dee0f7 122 * getDistance_cm() : get measured distance to the obstacle in cm
ommpy 0:d383e2dee0f7 123 * setRanges() : set new min and max distance range (boundaries are in datasheet)
ommpy 0:d383e2dee0f7 124 * getMinRange() : obtain current minimal distance to an obstacle
ommpy 0:d383e2dee0f7 125 * getMaxRange() : obtain current maximal distance to an obstacle
ommpy 0:d383e2dee0f7 126 * isNewDataReady() : check if the old data was overwritten (if it’s new)
ommpy 0:d383e2dee0f7 127 * startTimer() : a method to start the timer
ommpy 0:d383e2dee0f7 128 * stopTimer() : a method to stop the timer
ommpy 0:d383e2dee0f7 129 * init() : class initialization
ommpy 0:d383e2dee0f7 130 * turnOffTrigger() : a method that deactivates the trigger pin output
ommpy 0:d383e2dee0f7 131
ommpy 0:d383e2dee0f7 132 #### Fields
ommpy 0:d383e2dee0f7 133 * echo : interrupt pin connected to the sensor (input)
ommpy 0:d383e2dee0f7 134 * trigger : trigger signal is sent through this pin (output)
ommpy 0:d383e2dee0f7 135 * timer : timer instance that measures an input signal duration
ommpy 0:d383e2dee0f7 136 * distance : measured value of distance
ommpy 0:d383e2dee0f7 137 * minDistance : lower boundary of measurements
ommpy 0:d383e2dee0f7 138 * maxDistance : upper boundary of measurements
ommpy 0:d383e2dee0f7 139 * triggerTimeout : event on an output signal (to send a precise signal which the sensor needs)
ommpy 0:d383e2dee0f7 140 * echoTimeout : event on an input signal timeout (if the signal is out of measurement range)
ommpy 0:d383e2dee0f7 141 * newDataReady : indicator of new data
ommpy 0:d383e2dee0f7 142 * timerStarted : indicator of starting the timer
ommpy 0:d383e2dee0f7 143
ommpy 0:d383e2dee0f7 144 ## AS5045
ommpy 0:d383e2dee0f7 145 AS5045 library is needed to read a state of the AS5045 encoders through SPI. It contains two files : header and source. There are a class in the header that represents encoder reading implementation. Source file contains functions that read data from the encoders.
ommpy 0:d383e2dee0f7 146
ommpy 0:d383e2dee0f7 147 ![AS5045](doc/images/AS5045.png?raw=true "AS5045 Class Diagram")
ommpy 0:d383e2dee0f7 148
ommpy 0:d383e2dee0f7 149 ### Class description
ommpy 0:d383e2dee0f7 150 After SPI and chip select pin are set, the class can communicate with the encoder. To get the needed data getPosition() methods sets low the CS pin and sends dummy message in order to start communication. Data is received in two separate bytes (lower and upper parts) and then are united into one. getRotation() method returns angle of rotation of the encoder based on the position described earlier (angle can be calculated by multiplying a position with the encoder constant of degrees per one tick (change of position)).
ommpy 0:d383e2dee0f7 151
ommpy 0:d383e2dee0f7 152 #### Methods
ommpy 0:d383e2dee0f7 153 * AS5045() : assigns output pin to act like a chip select (cs) pin and sets up the SPI (pins and frequency)
ommpy 0:d383e2dee0f7 154 * getPosition() : sends dummy data through the SPI to read encoder’s position halved into two bytes from the encoder (that are joined together and returned)
ommpy 0:d383e2dee0f7 155 * getRotation() : converts current position of the encoder into rotation degrees (that are returned) by multiplying it with known amount of degrees per 1 position (is described in the datasheet)
ommpy 0:d383e2dee0f7 156
ommpy 0:d383e2dee0f7 157 #### Fields
ommpy 0:d383e2dee0f7 158 * \_spi : SPI instance that allows the class to communicate with the encoder
ommpy 0:d383e2dee0f7 159 * \_cs : chip select pin
ommpy 0:d383e2dee0f7 160 * MAX_VALUE : maximal value that can be transfered from the encoder (max position)
ommpy 0:d383e2dee0f7 161 * CONSTANT : change in angle of the encoder per position
ommpy 0:d383e2dee0f7 162
ommpy 0:d383e2dee0f7 163 ## E18_D80NK
ommpy 0:d383e2dee0f7 164 This library is used to communicate with the E18-D80NK infared sensor. The connection between Raspberry PI and the sensor is only one wire, which is set low, when there is an obstacle and high otherwise.
ommpy 0:d383e2dee0f7 165
ommpy 0:d383e2dee0f7 166 ![E18_D80NK](doc/images/E18_D80NK.png?raw=true "E18_D80NK Class Diagram")
ommpy 0:d383e2dee0f7 167
ommpy 0:d383e2dee0f7 168 ### Class description
ommpy 0:d383e2dee0f7 169 To start actually communicate with the sensor it's necessary to set the right pin (which is connected to the sensor's data pin). And then it's possible to check the state of the sensor using the checkObstacle() method, which returns 0 if there is an obstacle and 1 otherwise.
ommpy 0:d383e2dee0f7 170
ommpy 0:d383e2dee0f7 171 #### Methods
ommpy 0:d383e2dee0f7 172 * E18_D80NK() : assigns an input pin to communicate with the sensor
ommpy 0:d383e2dee0f7 173 * checkObstacle() : check if there is an obstacle in front of the sensor reading the state of the assigned pin
ommpy 0:d383e2dee0f7 174
ommpy 0:d383e2dee0f7 175 #### Fields
ommpy 0:d383e2dee0f7 176 * input : digital input pin to communicate with the sensor
ommpy 0:d383e2dee0f7 177
ommpy 0:d383e2dee0f7 178 ## Using
ommpy 0:d383e2dee0f7 179 In progress