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

Complete rewrite #13

Merged
merged 23 commits into from
Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
WIP
  • Loading branch information
ashtuchkin committed Feb 7, 2017
commit 4ea9cda23b26319893baad3e65e5672c33945615
8 changes: 4 additions & 4 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include <stdint.h>

// Tunable
const static int max_num_inputs = 8; // Number of concurrent sensors we support.
const static int max_bytes_in_data_frame = 50;
constexpr int max_num_inputs = 8; // Number of concurrent sensors we support.
constexpr int max_bytes_in_data_frame = 50;

// Not tunable: True for all systems.
const static int num_base_stations = 2;
const static int num_cycle_phases = 4;
constexpr int num_base_stations = 2;
constexpr int num_cycle_phases = 4;

// Pulses are generated by Inputs and processed by PulseProcessor
struct Pulse {
Expand Down
2 changes: 1 addition & 1 deletion src/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PointGeometryBuilder
};


static const int vec3d_size = 3;
constexpr int vec3d_size = 3;
typedef float vec3d[vec3d_size];

bool intersect_lines(const vec3d &orig1, const vec3d &vec1, const vec3d &orig2, const vec3d &vec2, vec3d *res, float *dist);
Expand Down
4 changes: 2 additions & 2 deletions src/input_cmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "settings.h"
#include "common.h"

const static int num_comparators = 3; // Number of Comparator modules in Teensy.
constexpr int num_comparators = 3; // Number of Comparator modules in Teensy.

// Statically allocated data structures.
InputCmpNode input_cmps[num_comparators]; // Nodes, by comparator #.
Expand All @@ -31,7 +31,7 @@ struct ComparatorInputPin {
uint32_t pin; // Teensy PIN
};

const static int num_input_pin_variants = 9;
constexpr int num_input_pin_variants = 9;
const static ComparatorInputPin input_pin_variants[num_input_pin_variants] = {
{0, 0, 11}, // CMP0_IN0 = Pin 11
{0, 1, 12}, // CMP0_IN1 = Pin 12
Expand Down
3 changes: 2 additions & 1 deletion src/mavlink.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "outputs.h"

// This strange sequence of includes and definitions is due to how mavlink_helpers.h works :(
// TODO: This currently includes code to sign packets with SHA256, bloating the binary. Get rid of it.
#define MAVLINK_USE_CONVENIENCE_FUNCTIONS
#define MAVLINK_COMM_NUM_BUFFERS 4
#define MAVLINK_SEND_UART_BYTES mavlink_send_uart_bytes
Expand All @@ -22,7 +23,7 @@ void mavlink_send_uart_bytes(mavlink_channel_t chan, const uint8_t *chars, unsig
#include "primitives/vector.h"

// Static list of streams we will output to.
Vector<Print *, MAVLINK_COMM_NUM_BUFFERS> output_streams;
static Vector<Print *, MAVLINK_COMM_NUM_BUFFERS> output_streams;

// Send multiple chars (uint8_t) over a comm channel
void mavlink_send_uart_bytes(mavlink_channel_t chan, const uint8_t *chars, unsigned length) {
Expand Down
4 changes: 2 additions & 2 deletions src/primitives/string_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "string_utils.h"
#include <Arduino.h>

static const int max_input_str_len = 256;
static const int max_words = 64;
constexpr int max_input_str_len = 256;
constexpr int max_words = 64;

// Non-blocking version of Stream.readBytesUntil('\n', ...). Returns line if found, or NULL if no line.
char *read_line(Stream &stream) {
Expand Down
56 changes: 28 additions & 28 deletions src/primitives/timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ enum TimeUnit {
class TimeDelta {
public:
// Constructors
inline TimeDelta(): time_delta_(0) {} // Default constructor to make it possible to include it in structs.
inline TimeDelta(int val, TimeUnit tu): time_delta_(val * tu) {} // Main way to create TimeDelta: value and TimeUnit.
constexpr TimeDelta(): time_delta_(0) {} // Default constructor to make it possible to include it in structs.
constexpr TimeDelta(int val, TimeUnit tu): time_delta_(val * tu) {} // Main way to create TimeDelta: value and TimeUnit.

inline int get_value(TimeUnit tu) const { return time_delta_ / tu; }
constexpr int get_value(TimeUnit tu) const { return time_delta_ / tu; }

// Simple comparison operators. Note: we only allow comparison between TimeDeltas.
inline bool operator<(const TimeDelta& delta) const { return time_delta_ < delta.time_delta_; }
inline bool operator>(const TimeDelta& delta) const { return time_delta_ > delta.time_delta_; }
inline bool operator<=(const TimeDelta& delta) const { return time_delta_ <= delta.time_delta_; }
inline bool operator>=(const TimeDelta& delta) const { return time_delta_ >= delta.time_delta_; }
inline bool operator==(const TimeDelta& delta) const { return time_delta_ == delta.time_delta_; }
inline bool operator!=(const TimeDelta& delta) const { return time_delta_ != delta.time_delta_; }
constexpr bool operator<(const TimeDelta& delta) const { return time_delta_ < delta.time_delta_; }
constexpr bool operator>(const TimeDelta& delta) const { return time_delta_ > delta.time_delta_; }
constexpr bool operator<=(const TimeDelta& delta) const { return time_delta_ <= delta.time_delta_; }
constexpr bool operator>=(const TimeDelta& delta) const { return time_delta_ >= delta.time_delta_; }
constexpr bool operator==(const TimeDelta& delta) const { return time_delta_ == delta.time_delta_; }
constexpr bool operator!=(const TimeDelta& delta) const { return time_delta_ != delta.time_delta_; }

// Simple arithmetic operators.
inline TimeDelta operator+(const TimeDelta& delta) const { return time_delta_ + delta.time_delta_; }
inline TimeDelta operator-(const TimeDelta& delta) const { return time_delta_ - delta.time_delta_; }
inline TimeDelta operator*(int val) const { return time_delta_ * val; }
inline TimeDelta operator/(int val) const { return time_delta_ / val; }
inline float operator/(const TimeDelta& delta) const {return float(time_delta_) / float(delta.time_delta_); }
constexpr TimeDelta operator+(const TimeDelta& delta) const { return time_delta_ + delta.time_delta_; }
constexpr TimeDelta operator-(const TimeDelta& delta) const { return time_delta_ - delta.time_delta_; }
constexpr TimeDelta operator*(int val) const { return time_delta_ * val; }
constexpr TimeDelta operator/(int val) const { return time_delta_ / val; }
constexpr float operator/(const TimeDelta& delta) const {return float(time_delta_) / float(delta.time_delta_); }

// Arithmetic assignment operators.
inline TimeDelta &operator+=(const TimeDelta& delta) { time_delta_ += delta.time_delta_; return *this; }
Expand All @@ -47,7 +47,7 @@ class TimeDelta {
}

private:
inline TimeDelta(int delta): time_delta_(delta) {}
constexpr TimeDelta(int delta): time_delta_(delta) {}
int time_delta_;
friend class Timestamp;
};
Expand All @@ -57,33 +57,33 @@ class TimeDelta {
class Timestamp {
public:
// Constructors.
inline Timestamp(): time_(0) {} // Default constructor. We need it to be able to keep Timestamp in structs.
inline Timestamp(const Timestamp& other): time_(other.time_) {}
constexpr Timestamp(): time_(0) {} // Default constructor. We need it to be able to keep Timestamp in structs.
constexpr Timestamp(const Timestamp& other): time_(other.time_) {}
inline Timestamp& operator=(const Timestamp& other) { time_ = other.time_; return *this; }
inline unsigned int get_value_unsafe(TimeUnit tu) const { return time_ / tu; }
constexpr unsigned int get_value_unsafe(TimeUnit tu) const { return time_ / tu; }

// Static getters.
static Timestamp cur_time(); // Implementation will try to get the best resolution possible.

// Create TimeDelta from a pair of Timestamps. Note that the wrapping is handled here correctly as we're converting to a signed int.
inline TimeDelta operator-(const Timestamp& other) const { return time_ - other.time_; }
constexpr TimeDelta operator-(const Timestamp& other) const { return time_ - other.time_; }

// Simple arithmetic operators with TimeDelta-s. Both operands are converted to uint32_t
inline Timestamp operator+(const TimeDelta& delta) const { return time_ + delta.time_delta_; }
inline Timestamp operator-(const TimeDelta& delta) const { return time_ - delta.time_delta_; }
constexpr Timestamp operator+(const TimeDelta& delta) const { return time_ + delta.time_delta_; }
constexpr Timestamp operator-(const TimeDelta& delta) const { return time_ - delta.time_delta_; }
inline Timestamp &operator+=(const TimeDelta& delta) { time_ += delta.time_delta_; return *this; }
inline Timestamp &operator-=(const TimeDelta& delta) { time_ -= delta.time_delta_; return *this; }

// Simple wrapping-aware comparison operators using conversion to signed int.
inline bool operator< (const Timestamp& other) const { return int(time_ - other.time_) < 0; }
inline bool operator> (const Timestamp& other) const { return int(time_ - other.time_) > 0; }
inline bool operator<=(const Timestamp& other) const { return int(time_ - other.time_) <= 0; }
inline bool operator>=(const Timestamp& other) const { return int(time_ - other.time_) >= 0; }
inline bool operator==(const Timestamp& other) const { return int(time_ - other.time_) == 0; }
inline bool operator!=(const Timestamp& other) const { return int(time_ - other.time_) != 0; }
constexpr bool operator< (const Timestamp& other) const { return int(time_ - other.time_) < 0; }
constexpr bool operator> (const Timestamp& other) const { return int(time_ - other.time_) > 0; }
constexpr bool operator<=(const Timestamp& other) const { return int(time_ - other.time_) <= 0; }
constexpr bool operator>=(const Timestamp& other) const { return int(time_ - other.time_) >= 0; }
constexpr bool operator==(const Timestamp& other) const { return int(time_ - other.time_) == 0; }
constexpr bool operator!=(const Timestamp& other) const { return int(time_ - other.time_) != 0; }

private:
inline Timestamp(unsigned int time): time_(time) {}
constexpr Timestamp(unsigned int time): time_(time) {}
unsigned int time_; // Think about this as some global time mod 2^32.
};

Expand Down
24 changes: 12 additions & 12 deletions src/pulse_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#include "message_logging.h"

// Pulse classification parameters.
const static TimeDelta min_short_pulse_len(2, usec);
const static TimeDelta min_long_pulse_len(40, usec);
const static TimeDelta max_long_pulse_len(300, usec);

const static TimeDelta long_pulse_starts_accepted_range(20, usec);
const static TimeDelta long_pulse_starts[num_base_stations] = {TimeDelta(0, usec), TimeDelta(400, usec)};

const static TimeDelta cycle_period(8333, usec); // Total len of 1 cycle.
const static TimeDelta angle_center_len(4000, usec);
const static TimeDelta short_pulse_min_time = angle_center_len - cycle_period / 3;
const static TimeDelta short_pulse_max_time = angle_center_len + cycle_period / 3;
const static TimeDelta cycle_processing_point = short_pulse_max_time + TimeDelta(100, usec); // time from start of the cycle.
constexpr TimeDelta min_short_pulse_len(2, usec);
constexpr TimeDelta min_long_pulse_len(40, usec);
constexpr TimeDelta max_long_pulse_len(300, usec);

constexpr TimeDelta long_pulse_starts_accepted_range(20, usec);
constexpr TimeDelta long_pulse_starts[num_base_stations] = {TimeDelta(0, usec), TimeDelta(400, usec)};

constexpr TimeDelta cycle_period(8333, usec); // Total len of 1 cycle.
constexpr TimeDelta angle_center_len(4000, usec);
constexpr TimeDelta short_pulse_min_time = angle_center_len - cycle_period / 3;
constexpr TimeDelta short_pulse_max_time = angle_center_len + cycle_period / 3;
constexpr TimeDelta cycle_processing_point = short_pulse_max_time + TimeDelta(100, usec); // time from start of the cycle.

enum CycleFixLevels {
kCycleFixNone = 0,
Expand Down
4 changes: 2 additions & 2 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "input_cmp.h"
#include "primitives/string_utils.h"

const static uint32_t current_settings_version = 0xbabe0001;
static uint32_t * const eeprom_addr = 0;
constexpr uint32_t current_settings_version = 0xbabe0001;
constexpr uint32_t * eeprom_addr = 0;
/* Example settings
reset
i0 pin 12 positive
Expand Down
5 changes: 3 additions & 2 deletions teensy-arm.toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ set_property(CACHE TEENSY_USB_MODE PROPERTY STRINGS SERIAL HID SERIAL_HID MIDI R
set(TARGET_FLAGS "-mcpu=cortex-m4 -mthumb -mfp16-format=ieee")
set(OPTIMIZE_FLAGS "-O2" CACHE STRING "Optimization flags") # Remember to reset cache and rebuild cmake when changing this.
set(CMAKE_C_FLAGS "${OPTIMIZE_FLAGS} -Wall -nostdlib -ffunction-sections -fdata-sections ${TARGET_FLAGS}" CACHE STRING "C/C++ flags")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -fno-exceptions -fno-rtti -felide-constructors -std=c++11 -fsingle-precision-constant" CACHE STRING "c++ flags")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=gnu++14 -fno-exceptions -fno-rtti -felide-constructors -fsingle-precision-constant -Woverloaded-virtual" CACHE STRING "c++ flags")

link_libraries(m)
set(LINKER_FLAGS "--gc-sections,--relax,--defsym=__rtc_localtime=0" CACHE STRING "Ld flags")
Expand Down Expand Up @@ -114,7 +114,8 @@ function(add_firmware_targets TARGET_NAME)

add_custom_target(${TARGET_NAME}_Assembler
COMMAND ${CMAKE_OBJDUMP} --demangle --disassemble --headers --wide $<TARGET_FILE_NAME:${TARGET_NAME}> > ${CMAKE_SOURCE_DIR}/build/source.S
COMMAND ${CMAKE_OBJDUMP} --demangle --syms $<TARGET_FILE_NAME:${TARGET_NAME}> | sort > ${CMAKE_SOURCE_DIR}/build/source_symbols.txt
COMMAND ${CMAKE_OBJDUMP} --demangle --disassemble --source --wide $<TARGET_FILE_NAME:${TARGET_NAME}> > ${CMAKE_SOURCE_DIR}/build/source-with-text.S
COMMAND ${CMAKE_OBJDUMP} --demangle --syms $<TARGET_FILE_NAME:${TARGET_NAME}> | sort > ${CMAKE_SOURCE_DIR}/build/source-symbols.txt
)

# See https://github.com/Koromix/ty
Expand Down