NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
pmc_actuator.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 PMC_ACTUATOR_PMC_ACTUATOR_H_
20 #define PMC_ACTUATOR_PMC_ACTUATOR_H_
21 
22 #include <i2c/i2c_new.h>
23 
24 #include <string>
25 #include <atomic>
26 #include <mutex>
27 #include <thread>
28 
29 namespace pmc_actuator {
30 
31 enum CmdMode { SHUTDOWN = 0, NORMAL = 1, RESTART = 2 };
32 
34 // This must be kept in sync with the protocol declared in the PMC firmware //
36 
37 constexpr int kNumNozzles = 6;
38 constexpr size_t kCommandMsgLength = 10;
39 constexpr size_t kTemperatureSensorsCount = 7;
40 constexpr size_t kTelemetryMsgLength = 16;
41 constexpr size_t kGitHashLength = 40;
42 constexpr size_t kDateLength = 17;
43 constexpr size_t kMaxMetadataLength = 64;
44 constexpr uint8_t kMetadataTypePrototype = 0x1;
45 constexpr uint8_t kMetadataTypeFlight = 0x2;
46 
47 typedef struct __attribute__((packed)) {
48  uint8_t motor_speed;
49  uint8_t nozzle_positions[kNumNozzles];
50  uint8_t mode;
51  uint8_t command_id;
52  uint8_t checksum;
54 
55 typedef union {
56  uint8_t asUint8;
57  struct {
58  uint8_t mode :2;
59  uint8_t stateStatus :3;
60  uint8_t mcDisabled :1;
61  uint8_t errBadCRC :1;
62  uint8_t errNozCmdOOR :1;
63  };
64 } Status1;
65 
66 typedef union {
67  uint8_t asUint8;
68  struct {
69  uint8_t errOvrCurrent :1;
70  uint8_t speedCamState :1;
71  uint8_t control :1;
72  uint8_t metadata :1;
73  uint8_t reserved :4;
74  };
75 } Status2;
76 
77 typedef struct __attribute__((packed)) {
78  uint8_t motor_speed;
79  uint8_t motor_current;
80  uint8_t v6_current;
81  uint16_t pressure;
82  uint8_t temperatures[kTemperatureSensorsCount];
83  Status1 status_1;
84  Status2 status_2;
85  uint8_t command_id;
86  uint8_t checksum;
88 
89 // A metadata frame containing the git hash and time of compilation
90 typedef struct __attribute__((packed)) {
91  uint8_t type; // 0x1 for METADATA_FIRMWARE
92  uint8_t hash[20]; // 20 byte hash
93  uint32_t time; // Unix timestamp
94  uint8_t checksum;
96 
100 
105  public:
106  // Sends the command packet.
107  virtual bool SendCommand(const Command &command) = 0;
108 
109  // Gets the telemetry packet.
110  virtual bool GetTelemetry(Telemetry *telemetry) = 0;
111 
112  virtual i2c::Address GetAddress() = 0;
113 
114  // Get the firmware version
115  virtual bool GetFirmwareHash(std::string & hash) = 0;
116 
117  // Get the firmware date
118  virtual bool GetFirmwareTime(std::string & date) = 0;
119 
120  // Get the firmware type
121  virtual bool GetFirmwareType(uint8_t & type) = 0;
122 
123  // Print out telemetry
124  void PrintTelemetry(std::ostream &out, const Telemetry &telem);
125 };
126 
130 class PmcActuator : public PmcActuatorBase {
131  private:
132  i2c::Device i2c_dev_;
133  bool metadata_received_; // Do we have valid metadata?
134  uint16_t metadata_index_; // Current metadata bit
135  uint8_t metadata_buffer_[kMaxMetadataLength]; // Buffer for metadata
136  MetadataVersion metadata_; // Last complete metadata
137 
138  public:
139  explicit PmcActuator(const i2c::Device &i2c_dev);
140  virtual ~PmcActuator(void);
141 
142  // Sends the command packet to the PMC over I2C.
143  bool SendCommand(const Command &command);
144 
145  // Gets the telemetry packet from the PMC over I2C.
146  bool GetTelemetry(Telemetry *telemetry);
147 
148  // Get the firmware version
149  bool GetFirmwareHash(std::string & hash);
150 
151  // Get the firmware date
152  bool GetFirmwareTime(std::string & date);
153 
154  // Get the firmware type
155  bool GetFirmwareType(uint8_t & type);
156 
157  // Get the i2c address
159 
160  private:
161  uint8_t ComputeChecksum(const uint8_t *buf, size_t size);
162 };
163 
165  private:
166  i2c::Address addr_;
167  std::fstream &cmds_output_;
168  std::fstream &telem_output_;
169  Command command_;
170  Telemetry telemetry_;
171 
172  public:
173  explicit PmcActuatorStub(int addr, std::fstream &cmds_out,
174  std::fstream &telem_out);
175  virtual ~PmcActuatorStub();
176 
177  // Sends the command packet to the PMC over I2C.
178  bool SendCommand(const Command &command);
179 
180  // Gets the telemetry packet from the PMC over I2C.
181  bool GetTelemetry(Telemetry *telemetry);
182 
183  // Get the firmware version
184  bool GetFirmwareHash(std::string & hash);
185 
186  // Get the firmware date
187  bool GetFirmwareTime(std::string & date);
188 
189  // Get the firmware date
190  bool GetFirmwareType(uint8_t & type);
191 
193 };
194 
195 } // namespace pmc_actuator
196 
197 #endif // PMC_ACTUATOR_PMC_ACTUATOR_H_
mode
uint8_t mode
Definition: signal_lights.h:74
pmc_actuator::PmcActuator::GetFirmwareType
bool GetFirmwareType(uint8_t &type)
Definition: pmc_actuator.cc:134
pmc_actuator::kMetadataTypePrototype
constexpr uint8_t kMetadataTypePrototype
Definition: pmc_actuator.h:44
pmc_actuator::Status2::errOvrCurrent
uint8_t errOvrCurrent
Definition: pmc_actuator.h:69
pmc_actuator::kGitHashLength
constexpr size_t kGitHashLength
Definition: pmc_actuator.h:41
pmc_actuator::PmcActuator::GetAddress
i2c::Address GetAddress()
Definition: pmc_actuator.cc:148
pmc_actuator::kTemperatureSensorsCount
constexpr size_t kTemperatureSensorsCount
Definition: pmc_actuator.h:39
i2c::Address
std::uint16_t Address
Definition: i2c_new.h:31
pmc_actuator::kMaxMetadataLength
constexpr size_t kMaxMetadataLength
Definition: pmc_actuator.h:43
pmc_actuator::PmcActuatorBase::GetFirmwareTime
virtual bool GetFirmwareTime(std::string &date)=0
pmc_actuator::PmcActuatorStub::GetFirmwareTime
bool GetFirmwareTime(std::string &date)
Definition: pmc_actuator.cc:207
pmc_actuator
Definition: pmc_actuator.h:29
pmc_actuator::Status2::reserved
uint8_t reserved
Definition: pmc_actuator.h:73
pmc_actuator::SHUTDOWN
@ SHUTDOWN
Definition: pmc_actuator.h:31
pmc_actuator::Status1::mcDisabled
uint8_t mcDisabled
Definition: pmc_actuator.h:60
pmc_actuator::Status1::mode
uint8_t mode
Definition: pmc_actuator.h:58
pmc_actuator::__attribute__
struct __attribute__((packed))
Definition: pmc_actuator.h:47
pmc_actuator::Command
Command
Definition: pmc_actuator.h:53
pmc_actuator::kTelemetryMsgLength
constexpr size_t kTelemetryMsgLength
Definition: pmc_actuator.h:40
pmc_actuator::Telemetry
Telemetry
Definition: pmc_actuator.h:87
pmc_actuator::PmcActuator::GetFirmwareHash
bool GetFirmwareHash(std::string &hash)
Definition: pmc_actuator.cc:112
pmc_actuator::PmcActuatorBase::GetFirmwareHash
virtual bool GetFirmwareHash(std::string &hash)=0
pmc_actuator::Status2::control
uint8_t control
Definition: pmc_actuator.h:71
pmc_actuator::PmcActuatorStub::~PmcActuatorStub
virtual ~PmcActuatorStub()
Definition: pmc_actuator.cc:158
pmc_actuator::kCommandMsgLength
constexpr size_t kCommandMsgLength
Definition: pmc_actuator.h:38
pmc_actuator::PmcActuator::GetTelemetry
bool GetTelemetry(Telemetry *telemetry)
Definition: pmc_actuator.cc:58
pmc_actuator::PmcActuatorBase
Definition: pmc_actuator.h:104
pmc_actuator::Status1::errNozCmdOOR
uint8_t errNozCmdOOR
Definition: pmc_actuator.h:62
pmc_actuator::NORMAL
@ NORMAL
Definition: pmc_actuator.h:31
pmc_actuator::kDateLength
constexpr size_t kDateLength
Definition: pmc_actuator.h:42
pmc_actuator::MetadataVersion
MetadataVersion
Definition: pmc_actuator.h:95
pmc_actuator::PmcActuator::GetFirmwareTime
bool GetFirmwareTime(std::string &date)
Definition: pmc_actuator.cc:123
pmc_actuator::PmcActuatorBase::SendCommand
virtual bool SendCommand(const Command &command)=0
pmc_actuator::PmcActuator::PmcActuator
PmcActuator(const i2c::Device &i2c_dev)
Definition: pmc_actuator.cc:30
pmc_actuator::Status2::speedCamState
uint8_t speedCamState
Definition: pmc_actuator.h:70
pmc_actuator::PmcActuatorStub::GetFirmwareType
bool GetFirmwareType(uint8_t &type)
Definition: pmc_actuator.cc:213
pmc_actuator::Status1::stateStatus
uint8_t stateStatus
Definition: pmc_actuator.h:59
checksum
uint8_t checksum(uint8_t *buf, size_t len)
Definition: fam_cmd_i2c_node.cc:53
pmc_actuator::PmcActuatorBase::PrintTelemetry
void PrintTelemetry(std::ostream &out, const Telemetry &telem)
Definition: pmc_actuator.cc:222
pmc_actuator::kNumNozzles
constexpr int kNumNozzles
Definition: pmc_actuator.h:37
pmc_actuator::PmcActuatorBase::GetFirmwareType
virtual bool GetFirmwareType(uint8_t &type)=0
i2c_new.h
pmc_actuator::RESTART
@ RESTART
Definition: pmc_actuator.h:31
pmc_actuator::Status2::asUint8
uint8_t asUint8
Definition: pmc_actuator.h:67
pmc_actuator::Status2::metadata
uint8_t metadata
Definition: pmc_actuator.h:72
pmc_actuator::PmcActuatorStub::GetTelemetry
bool GetTelemetry(Telemetry *telemetry)
Definition: pmc_actuator.cc:183
pmc_actuator::PmcActuatorStub::GetFirmwareHash
bool GetFirmwareHash(std::string &hash)
Definition: pmc_actuator.cc:201
pmc_actuator::CmdMode
CmdMode
Definition: pmc_actuator.h:31
pmc_actuator::PmcActuator::SendCommand
bool SendCommand(const Command &command)
Definition: pmc_actuator.cc:35
pmc_actuator::PmcActuatorBase::GetTelemetry
virtual bool GetTelemetry(Telemetry *telemetry)=0
pmc_actuator::Status1::asUint8
uint8_t asUint8
Definition: pmc_actuator.h:56
pmc_actuator::PmcActuatorStub
Definition: pmc_actuator.h:164
i2c::Device
Definition: i2c_new.h:82
pmc_actuator::Status1::errBadCRC
uint8_t errBadCRC
Definition: pmc_actuator.h:61
pmc_actuator::PmcActuatorBase::GetAddress
virtual i2c::Address GetAddress()=0
pmc_actuator::PmcActuator::~PmcActuator
virtual ~PmcActuator(void)
Definition: pmc_actuator.cc:33
pmc_actuator::PmcActuatorStub::PmcActuatorStub
PmcActuatorStub(int addr, std::fstream &cmds_out, std::fstream &telem_out)
Definition: pmc_actuator.cc:152
pmc_actuator::PmcActuatorStub::GetAddress
i2c::Address GetAddress()
Definition: pmc_actuator.cc:218
pmc_actuator::kMetadataTypeFlight
constexpr uint8_t kMetadataTypeFlight
Definition: pmc_actuator.h:45
pmc_actuator::PmcActuatorStub::SendCommand
bool SendCommand(const Command &command)
Definition: pmc_actuator.cc:162
pmc_actuator::PmcActuator
Definition: pmc_actuator.h:130