NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
signal_lights.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef SIGNAL_LIGHTS_SIGNAL_LIGHTS_H_
20 #define SIGNAL_LIGHTS_SIGNAL_LIGHTS_H_
21 
22 #include <functional>
23 #include <string>
24 
25 #include "../custom_i2c/i2c.h"
26 
27 namespace signal_lights {
28 
29 // I2C COMMUNICATION PROTOCOL
30 
31 #define NUM_LEDS 44
32 #define NUM_BLOCKS 4
33 #define BLOCK_SIZE 11
34 
35 typedef enum {
39 } LEDC_MODE;
40 
41 typedef enum {
50 } LEDC_STATE;
51 
52 typedef enum {
57 } LEDC_ERROR;
58 
59 typedef union {
60  uint16_t raw;
61  struct {
62  uint8_t red : 5; // Red bits
63  uint8_t green : 5; // Green bits
64  uint8_t blue : 5; // Blue bits
65  uint8_t ignore : 1; // Ignore this pixel
66  } __attribute__((packed));
67 } LEDC_COMMAND_COLOR;
68 
69 typedef union {
70  uint8_t raw;
71  struct {
72  uint8_t render : 1; // Render the LEDs after this packet
73  uint8_t mask : 4; // Blocks - 0: 0:10, 0: 11:21, 0: 22:32, 3: 33:43
74  uint8_t range : 2; // Range k (0: 255, 1: 128, 2: 64: 3: 32)
75  uint8_t mode : 1; // Operating mode
76  } __attribute__((packed));
77 } LEDC_COMMAND_INSTRUCTION;
78 
79 typedef struct __attribute__((packed)) {
80  LEDC_COMMAND_INSTRUCTION instruction; // Instruction (1)
81  LEDC_COMMAND_COLOR pixels[BLOCK_SIZE]; // LED block values (22)
82  uint8_t checksum; // Checksum (1)
84 
85 typedef union {
86  uint8_t raw;
87  struct {
88  uint8_t control : 1; // Firmware version reset bit
89  uint8_t metadata : 1; // Firmware version data bit
90  uint8_t error : 3; // Packet errors - DROP, OVERRUN, CRC
91  uint8_t state : 3; // Current state
92  } __attribute__((packed));
93 } LEDC_TELEMETRY_STATUS;
94 
95 typedef struct __attribute__((packed)) {
96  LEDC_TELEMETRY_STATUS status;
97  uint8_t checksum;
99 
100 typedef enum {
103 } METADATA_TYPE;
104 
105 typedef struct __attribute__((packed)) {
106  uint8_t type; // 0x1 for METADATA_FIRMWARE
107  uint8_t hash[20]; // 20 byte hash
108  uint32_t time; // Unix timestamp
109  uint8_t chksum;
111 
112 class Device {
113  public:
114  Device(const char *bus, uint8_t addr);
115  int read(uint8_t *buf, unsigned int length);
116  int write(uint8_t *buf, unsigned int length);
117 
118  private:
119  int handle;
120 };
121 
122 // A single signal light system
124  public:
125  static constexpr double kRenderTime_secs_ = 50e-6;
126 
127  // Constructor
128  explicit SignalLights(const Device &i2c_dev);
129 
130  // Destructor
131  virtual ~SignalLights();
132 
133  // Configure and return data
134  bool Set(uint8_t pos, uint8_t red, uint8_t green, uint8_t blue);
135 
136  void SetAll(uint8_t red, uint8_t green, uint8_t blue);
137 
138  // Get the polling frequency for a desired control rate
139  double GetPollDuration(double rate);
140 
141  // Poll a given block and return if metadata was received
142  bool Poll();
143 
144  // Get the firmware hash ans build time
145  std::string GetHash() { return hash_; }
146  std::string GetTime() { return time_; }
147 
148  private:
149  // SignalLights(const SignalLights &sl) {
150  // // bouh
151  // }
152 
153  uint16_t Read(uint8_t *buff, uint16_t len);
154  uint16_t Read(uint8_t *buff);
155  uint16_t Write(uint8_t *buff, uint16_t len);
156  uint8_t ComputeChecksum(uint8_t *buf, size_t size);
157 
158  // Local parameters
159  Device i2c_dev_;
160  LEDC_COMMAND command_[NUM_BLOCKS];
161  LEDC_TELEMETRY telemetry_;
162  METADATA_VERSION metadata_;
163  size_t block_index_, metadata_index_;
164  std::string hash_, time_;
165 };
166 
167 } // namespace signal_lights
168 
169 #endif // SIGNAL_LIGHTS_SIGNAL_LIGHTS_H_
signal_lights
Definition: signal_lights.h:27
range
uint8_t range
Definition: signal_lights.h:73
signal_lights::LEDC_ERROR
LEDC_ERROR
Definition: signal_lights.h:52
signal_lights::LEDC_STATE_ERROR
@ LEDC_STATE_ERROR
Definition: signal_lights.h:49
BLOCK_SIZE
#define BLOCK_SIZE
Definition: signal_lights.h:33
signal_lights::LEDC_MODE_SHUTDOWN
@ LEDC_MODE_SHUTDOWN
Definition: signal_lights.h:36
signal_lights::LEDC_TELEMETRY_STATUS
Definition: signal_lights.h:85
signal_lights::SignalLights::GetHash
std::string GetHash()
Definition: signal_lights.h:145
signal_lights::SignalLights::SetAll
void SetAll(uint8_t red, uint8_t green, uint8_t blue)
Definition: signal_lights.cc:74
signal_lights::LEDC_MODE_NOMINAL
@ LEDC_MODE_NOMINAL
Definition: signal_lights.h:37
signal_lights::SignalLights
Definition: signal_lights.h:123
signal_lights::LEDC_ERROR_PACKET_CRC
@ LEDC_ERROR_PACKET_CRC
Definition: signal_lights.h:54
signal_lights::LEDC_TELEMETRY_STATUS::state
uint8_t state
Definition: signal_lights.h:91
signal_lights::LEDC_TELEMETRY_STATUS::raw
uint8_t raw
Definition: signal_lights.h:86
signal_lights::LEDC_MODE
LEDC_MODE
Definition: signal_lights.h:35
signal_lights::LEDC_STATE_OOR_STATE
@ LEDC_STATE_OOR_STATE
Definition: signal_lights.h:43
signal_lights::METADATA_TYPE
METADATA_TYPE
Definition: signal_lights.h:100
signal_lights::Device
Definition: signal_lights.h:112
signal_lights::LEDC_MODE_INIT
@ LEDC_MODE_INIT
Definition: signal_lights.h:38
signal_lights::LEDC_STATE_RESERVED
@ LEDC_STATE_RESERVED
Definition: signal_lights.h:47
render
uint8_t render
Definition: signal_lights.h:71
signal_lights::LEDC_COMMAND
LEDC_COMMAND
Definition: signal_lights.h:83
mask
uint8_t mask
Definition: signal_lights.h:72
signal_lights::SignalLights::~SignalLights
virtual ~SignalLights()
Definition: signal_lights.cc:58
signal_lights::LEDC_STATE_MODE_CHANGE_TO_NOMINAL
@ LEDC_STATE_MODE_CHANGE_TO_NOMINAL
Definition: signal_lights.h:45
signal_lights::METADATA_TYPE_VERSION
@ METADATA_TYPE_VERSION
Definition: signal_lights.h:101
signal_lights::LEDC_ERROR_NONE
@ LEDC_ERROR_NONE
Definition: signal_lights.h:53
signal_lights::LEDC_ERROR_OVERRUN
@ LEDC_ERROR_OVERRUN
Definition: signal_lights.h:56
NUM_BLOCKS
#define NUM_BLOCKS
Definition: signal_lights.h:32
signal_lights::SignalLights::GetPollDuration
double GetPollDuration(double rate)
Definition: signal_lights.cc:86
signal_lights::LEDC_STATE_PIC_STARTUP
@ LEDC_STATE_PIC_STARTUP
Definition: signal_lights.h:48
signal_lights::SignalLights::Set
bool Set(uint8_t pos, uint8_t red, uint8_t green, uint8_t blue)
Definition: signal_lights.cc:61
control
uint8_t control
Definition: signal_lights.h:87
signal_lights::LEDC_COMMAND_COLOR::raw
uint16_t raw
Definition: signal_lights.h:60
signal_lights::LEDC_COMMAND_INSTRUCTION::raw
uint8_t raw
Definition: signal_lights.h:70
signal_lights::Device::Device
Device(const char *bus, uint8_t addr)
Definition: signal_lights.cc:30
signal_lights::SignalLights::GetTime
std::string GetTime()
Definition: signal_lights.h:146
signal_lights::Device::read
int read(uint8_t *buf, unsigned int length)
Definition: signal_lights.cc:34
signal_lights::SignalLights::SignalLights
SignalLights(const Device &i2c_dev)
Definition: signal_lights.cc:44
signal_lights::LEDC_TELEMETRY
LEDC_TELEMETRY
Definition: signal_lights.h:98
checksum
uint8_t checksum(uint8_t *buf, size_t len)
Definition: fam_cmd_i2c_node.cc:53
signal_lights::METADATA_VERSION
METADATA_VERSION
Definition: signal_lights.h:110
signal_lights::LEDC_STATE_MODE_CHANGE_TO_SHUTDOWN
@ LEDC_STATE_MODE_CHANGE_TO_SHUTDOWN
Definition: signal_lights.h:44
signal_lights::LEDC_COMMAND_INSTRUCTION
Definition: signal_lights.h:69
signal_lights::LEDC_STATE_NO_MODE_CHANGE
@ LEDC_STATE_NO_MODE_CHANGE
Definition: signal_lights.h:42
signal_lights::LEDC_ERROR_PACKET_DROPPED
@ LEDC_ERROR_PACKET_DROPPED
Definition: signal_lights.h:55
signal_lights::LEDC_STATE
LEDC_STATE
Definition: signal_lights.h:41
error
uint8_t error
Definition: signal_lights.h:89
signal_lights::SignalLights::kRenderTime_secs_
static constexpr double kRenderTime_secs_
Definition: signal_lights.h:125
signal_lights::LEDC_COMMAND_COLOR::ignore
uint8_t ignore
Definition: signal_lights.h:65
signal_lights::LEDC_STATE_TIMEOUT_INDUCED_SHUTDOWN
@ LEDC_STATE_TIMEOUT_INDUCED_SHUTDOWN
Definition: signal_lights.h:46
green
uint8_t green
Definition: signal_lights.h:62
signal_lights::METADATA_TYPE_INVALID
@ METADATA_TYPE_INVALID
Definition: signal_lights.h:102
signal_lights::LEDC_COMMAND_COLOR
Definition: signal_lights.h:59
signal_lights::LEDC_COMMAND_INSTRUCTION::mode
uint8_t mode
Definition: signal_lights.h:75
metadata
uint8_t metadata
Definition: signal_lights.h:88
signal_lights::Device::write
int write(uint8_t *buf, unsigned int length)
Definition: signal_lights.cc:38
blue
uint8_t blue
Definition: signal_lights.h:63
red
uint8_t red
Definition: signal_lights.h:61
signal_lights::SignalLights::Poll
bool Poll()
Definition: signal_lights.cc:91
signal_lights::__attribute__
class signal_lights::SignalPair __attribute__
Definition: signal_lights.h:95