You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					599 lines
				
				19 KiB
			
		
		
			
		
	
	
					599 lines
				
				19 KiB
			| 
								 
											1 year ago
										 
									 | 
							
								#include "selfdrive/pandad/pandad.h"
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								#include <algorithm>
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								#include <array>
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								#include <atomic>
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								#include <bitset>
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								#include <cassert>
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								#include <cerrno>
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								#include <chrono>
							 | 
						||
| 
								 | 
							
								#include <future>
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								#include <memory>
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								#include <thread>
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "cereal/gen/cpp/car.capnp.h"
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								#include "cereal/messaging/messaging.h"
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								#include "common/params.h"
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								#include "common/ratekeeper.h"
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								#include "common/swaglog.h"
							 | 
						||
| 
								 | 
							
								#include "common/timing.h"
							 | 
						||
| 
								 | 
							
								#include "common/util.h"
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								#include "system/hardware/hw.h"
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								// -- Multi-panda conventions --
							 | 
						||
| 
								 | 
							
								// Ordering:
							 | 
						||
| 
								 | 
							
								// - The internal panda will always be the first panda
							 | 
						||
| 
								 | 
							
								// - Consecutive pandas will be sorted based on panda type, and then serial number
							 | 
						||
| 
								 | 
							
								// Connecting:
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								// - If a panda connection is dropped, pandad will reconnect to all pandas
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								// - If a panda is added, we will only reconnect when we are offroad
							 | 
						||
| 
								 | 
							
								// CAN buses:
							 | 
						||
| 
								 | 
							
								// - Each panda will have it's block of 4 buses. E.g.: the second panda will use
							 | 
						||
| 
								 | 
							
								//   bus numbers 4, 5, 6 and 7
							 | 
						||
| 
								 | 
							
								// - The internal panda will always be used for accessing the OBD2 port,
							 | 
						||
| 
								 | 
							
								//   and thus firmware queries
							 | 
						||
| 
								 | 
							
								// Safety:
							 | 
						||
| 
								 | 
							
								// - SafetyConfig is a list, which is mapped to the connected pandas
							 | 
						||
| 
								 | 
							
								// - If there are more pandas connected than there are SafetyConfigs,
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								//   the excess pandas will remain in "silent" or "noOutput" mode
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								// Ignition:
							 | 
						||
| 
								 | 
							
								// - If any of the ignition sources in any panda is high, ignition is high
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#define MAX_IR_POWER 0.5f
							 | 
						||
| 
								 | 
							
								#define MIN_IR_POWER 0.0f
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								#define CUTOFF_IL 400
							 | 
						||
| 
								 | 
							
								#define SATURATE_IL 1000
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								using namespace std::chrono_literals;
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								std::atomic<bool> ignition(false);
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								ExitHandler do_exit;
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								bool check_all_connected(const std::vector<Panda *> &pandas) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  for (const auto& panda : pandas) {
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    if (!panda->connected()) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      do_exit = true;
							 | 
						||
| 
								 | 
							
								      return false;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  return true;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								bool safety_setter_thread(std::vector<Panda *> pandas) {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  LOGD("Starting safety setter thread");
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  Params p;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  // there should be at least one panda connected
							 | 
						||
| 
								 | 
							
								  if (pandas.size() == 0) {
							 | 
						||
| 
								 | 
							
								    return false;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  // initialize to ELM327 without OBD multiplexing for fingerprinting
							 | 
						||
| 
								 | 
							
								  bool obd_multiplexing_enabled = false;
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  for (int i = 0; i < pandas.size(); i++) {
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    pandas[i]->set_safety_model(cereal::CarParams::SafetyModel::ELM327, 1U);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  // openpilot can switch between multiplexing modes for different FW queries
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  while (true) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if (do_exit || !check_all_connected(pandas) || !ignition) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      return false;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    bool obd_multiplexing_requested = p.getBool("ObdMultiplexingEnabled");
							 | 
						||
| 
								 | 
							
								    if (obd_multiplexing_requested != obd_multiplexing_enabled) {
							 | 
						||
| 
								 | 
							
								      for (int i = 0; i < pandas.size(); i++) {
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								        const uint16_t safety_param = (i > 0 || !obd_multiplexing_requested) ? 1U : 0U;
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								        pandas[i]->set_safety_model(cereal::CarParams::SafetyModel::ELM327, safety_param);
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								      obd_multiplexing_enabled = obd_multiplexing_requested;
							 | 
						||
| 
								 | 
							
								      p.putBool("ObdMultiplexingChanged", true);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (p.getBool("FirmwareQueryDone")) {
							 | 
						||
| 
								 | 
							
								      LOGW("finished FW query");
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      break;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    util::sleep_for(20);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  std::string params;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  LOGW("waiting for params to set safety model");
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  while (true) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if (do_exit || !check_all_connected(pandas) || !ignition) {
							 | 
						||
| 
								 | 
							
								      return false;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    if (p.getBool("ControlsReady")) {
							 | 
						||
| 
								 | 
							
								      params = p.get("CarParams");
							 | 
						||
| 
								 | 
							
								      if (params.size() > 0) break;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    util::sleep_for(100);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  LOGW("got %lu bytes CarParams", params.size());
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  AlignedBuffer aligned_buf;
							 | 
						||
| 
								 | 
							
								  capnp::FlatArrayMessageReader cmsg(aligned_buf.align(params.data(), params.size()));
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  cereal::CarParams::Reader car_params = cmsg.getRoot<cereal::CarParams>();
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  cereal::CarParams::SafetyModel safety_model;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  uint16_t safety_param;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  auto safety_configs = car_params.getSafetyConfigs();
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  uint16_t alternative_experience = car_params.getAlternativeExperience();
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  for (uint32_t i = 0; i < pandas.size(); i++) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    auto panda = pandas[i];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (safety_configs.size() > i) {
							 | 
						||
| 
								 | 
							
								      safety_model = safety_configs[i].getSafetyModel();
							 | 
						||
| 
								 | 
							
								      safety_param = safety_configs[i].getSafetyParam();
							 | 
						||
| 
								 | 
							
								    } else {
							 | 
						||
| 
								 | 
							
								      // If no safety mode is specified, default to silent
							 | 
						||
| 
								 | 
							
								      safety_model = cereal::CarParams::SafetyModel::SILENT;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      safety_param = 0U;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    LOGW("panda %d: setting safety model: %d, param: %d, alternative experience: %d", i, (int)safety_model, safety_param, alternative_experience);
							 | 
						||
| 
								 | 
							
								    panda->set_alternative_experience(alternative_experience);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    panda->set_safety_model(safety_model, safety_param);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  return true;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								Panda *connect(std::string serial="", uint32_t index=0) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  std::unique_ptr<Panda> panda;
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  try {
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    panda = std::make_unique<Panda>(serial, (index * PANDA_BUS_OFFSET));
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  } catch (std::exception &e) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    return nullptr;
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  // common panda config
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  if (getenv("BOARDD_LOOPBACK")) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    panda->set_loopback(true);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  //panda->enable_deepsleep();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  if (!panda->up_to_date() && !getenv("BOARDD_SKIP_FW_CHECK")) {
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    throw std::runtime_error("Panda firmware out of date. Run pandad.py to update.");
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  return panda.release();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								void can_send_thread(std::vector<Panda *> pandas, bool fake_send) {
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  util::set_thread_name("pandad_can_send");
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  AlignedBuffer aligned_buf;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  std::unique_ptr<Context> context(Context::create());
							 | 
						||
| 
								 | 
							
								  std::unique_ptr<SubSocket> subscriber(SubSocket::create(context.get(), "sendcan"));
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  assert(subscriber != NULL);
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  subscriber->setTimeout(100);
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  // run as fast as messages come in
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  while (!do_exit && check_all_connected(pandas)) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    std::unique_ptr<Message> msg(subscriber->receive());
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if (!msg) {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      if (errno == EINTR) {
							 | 
						||
| 
								 | 
							
								        do_exit = true;
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								      continue;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    capnp::FlatArrayMessageReader cmsg(aligned_buf.align(msg.get()));
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    cereal::Event::Reader event = cmsg.getRoot<cereal::Event>();
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    // Don't send if older than 1 second
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if ((nanos_since_boot() - event.getLogMonoTime() < 1e9) && !fake_send) {
							 | 
						||
| 
								 | 
							
								      for (const auto& panda : pandas) {
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								        LOGT("sending sendcan to panda: %s", (panda->hw_serial()).c_str());
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        panda->can_send(event.getSendcan());
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								        LOGT("sendcan sent to panda: %s", (panda->hw_serial()).c_str());
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      }
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    } else {
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      LOGE("sendcan too old to send: %" PRIu64 ", %" PRIu64, nanos_since_boot(), event.getLogMonoTime());
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								void can_recv_thread(std::vector<Panda *> pandas) {
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  util::set_thread_name("pandad_can_recv");
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  PubMaster pm({"can"});
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  // run at 100Hz
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  RateKeeper rk("pandad_can_recv", 100);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  std::vector<can_frame> raw_can_data;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  while (!do_exit && check_all_connected(pandas)) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    bool comms_healthy = true;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    raw_can_data.clear();
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    for (const auto& panda : pandas) {
							 | 
						||
| 
								 | 
							
								      comms_healthy &= panda->can_receive(raw_can_data);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    MessageBuilder msg;
							 | 
						||
| 
								 | 
							
								    auto evt = msg.initEvent();
							 | 
						||
| 
								 | 
							
								    evt.setValid(comms_healthy);
							 | 
						||
| 
								 | 
							
								    auto canData = evt.initCan(raw_can_data.size());
							 | 
						||
| 
								 | 
							
								    for (uint i = 0; i<raw_can_data.size(); i++) {
							 | 
						||
| 
								 | 
							
								      canData[i].setAddress(raw_can_data[i].address);
							 | 
						||
| 
								 | 
							
								      canData[i].setBusTime(raw_can_data[i].busTime);
							 | 
						||
| 
								 | 
							
								      canData[i].setDat(kj::arrayPtr((uint8_t*)raw_can_data[i].dat.data(), raw_can_data[i].dat.size()));
							 | 
						||
| 
								 | 
							
								      canData[i].setSrc(raw_can_data[i].src);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    pm.send("can", msg);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    rk.keepTime();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								std::optional<bool> send_panda_states(PubMaster *pm, const std::vector<Panda *> &pandas, bool spoofing_started) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  bool ignition_local = false;
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  const uint32_t pandas_cnt = pandas.size();
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  // build msg
							 | 
						||
| 
								 | 
							
								  MessageBuilder msg;
							 | 
						||
| 
								 | 
							
								  auto evt = msg.initEvent();
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  auto pss = evt.initPandaStates(pandas_cnt);
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  std::vector<health_t> pandaStates;
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  pandaStates.reserve(pandas_cnt);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  std::vector<std::array<can_health_t, PANDA_CAN_CNT>> pandaCanStates;
							 | 
						||
| 
								 | 
							
								  pandaCanStates.reserve(pandas_cnt);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  const bool red_panda_comma_three = (pandas.size() == 2) &&
							 | 
						||
| 
								 | 
							
								                                     (pandas[0]->hw_type == cereal::PandaState::PandaType::DOS) &&
							 | 
						||
| 
								 | 
							
								                                     (pandas[1]->hw_type == cereal::PandaState::PandaType::RED_PANDA);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  for (const auto& panda : pandas){
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    auto health_opt = panda->get_state();
							 | 
						||
| 
								 | 
							
								    if (!health_opt) {
							 | 
						||
| 
								 | 
							
								      return std::nullopt;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    health_t health = *health_opt;
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    std::array<can_health_t, PANDA_CAN_CNT> can_health{};
							 | 
						||
| 
								 | 
							
								    for (uint32_t i = 0; i < PANDA_CAN_CNT; i++) {
							 | 
						||
| 
								 | 
							
								      auto can_health_opt = panda->get_can_state(i);
							 | 
						||
| 
								 | 
							
								      if (!can_health_opt) {
							 | 
						||
| 
								 | 
							
								        return std::nullopt;
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								      can_health[i] = *can_health_opt;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    pandaCanStates.push_back(can_health);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if (spoofing_started) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      health.ignition_line_pkt = 1;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    // on comma three setups with a red panda, the dos can
							 | 
						||
| 
								 | 
							
								    // get false positive ignitions due to the harness box
							 | 
						||
| 
								 | 
							
								    // without a harness connector, so ignore it
							 | 
						||
| 
								 | 
							
								    if (red_panda_comma_three && (panda->hw_type == cereal::PandaState::PandaType::DOS)) {
							 | 
						||
| 
								 | 
							
								      health.ignition_line_pkt = 0;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    ignition_local |= ((health.ignition_line_pkt != 0) || (health.ignition_can_pkt != 0));
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    pandaStates.push_back(health);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  for (uint32_t i = 0; i < pandas_cnt; i++) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    auto panda = pandas[i];
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    const auto &health = pandaStates[i];
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    // Make sure CAN buses are live: safety_setter_thread does not work if Panda CAN are silent and there is only one other CAN node
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if (health.safety_mode_pkt == (uint8_t)(cereal::CarParams::SafetyModel::SILENT)) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      panda->set_safety_model(cereal::CarParams::SafetyModel::NO_OUTPUT);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    bool power_save_desired = !ignition_local;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if (health.power_save_enabled_pkt != power_save_desired) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      panda->set_power_saving(power_save_desired);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // set safety mode to NO_OUTPUT when car is off. ELM327 is an alternative if we want to leverage athenad/connect
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if (!ignition_local && (health.safety_mode_pkt != (uint8_t)(cereal::CarParams::SafetyModel::NO_OUTPUT))) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      panda->set_safety_model(cereal::CarParams::SafetyModel::NO_OUTPUT);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    if (!panda->comms_healthy()) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      evt.setValid(false);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    auto ps = pss[i];
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    ps.setVoltage(health.voltage_pkt);
							 | 
						||
| 
								 | 
							
								    ps.setCurrent(health.current_pkt);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    ps.setUptime(health.uptime_pkt);
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    ps.setSafetyTxBlocked(health.safety_tx_blocked_pkt);
							 | 
						||
| 
								 | 
							
								    ps.setSafetyRxInvalid(health.safety_rx_invalid_pkt);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    ps.setIgnitionLine(health.ignition_line_pkt);
							 | 
						||
| 
								 | 
							
								    ps.setIgnitionCan(health.ignition_can_pkt);
							 | 
						||
| 
								 | 
							
								    ps.setControlsAllowed(health.controls_allowed_pkt);
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    ps.setTxBufferOverflow(health.tx_buffer_overflow_pkt);
							 | 
						||
| 
								 | 
							
								    ps.setRxBufferOverflow(health.rx_buffer_overflow_pkt);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    ps.setPandaType(panda->hw_type);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    ps.setSafetyModel(cereal::CarParams::SafetyModel(health.safety_mode_pkt));
							 | 
						||
| 
								 | 
							
								    ps.setSafetyParam(health.safety_param_pkt);
							 | 
						||
| 
								 | 
							
								    ps.setFaultStatus(cereal::PandaState::FaultStatus(health.fault_status_pkt));
							 | 
						||
| 
								 | 
							
								    ps.setPowerSaveEnabled((bool)(health.power_save_enabled_pkt));
							 | 
						||
| 
								 | 
							
								    ps.setHeartbeatLost((bool)(health.heartbeat_lost_pkt));
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    ps.setAlternativeExperience(health.alternative_experience_pkt);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    ps.setHarnessStatus(cereal::PandaState::HarnessStatus(health.car_harness_status_pkt));
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    ps.setInterruptLoad(health.interrupt_load_pkt);
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    ps.setFanPower(health.fan_power);
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    ps.setFanStallCount(health.fan_stall_count);
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    ps.setSafetyRxChecksInvalid((bool)(health.safety_rx_checks_invalid_pkt));
							 | 
						||
| 
								 | 
							
								    ps.setSpiChecksumErrorCount(health.spi_checksum_error_count_pkt);
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    ps.setSbu1Voltage(health.sbu1_voltage_mV / 1000.0f);
							 | 
						||
| 
								 | 
							
								    ps.setSbu2Voltage(health.sbu2_voltage_mV / 1000.0f);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    std::array<cereal::PandaState::PandaCanState::Builder, PANDA_CAN_CNT> cs = {ps.initCanState0(), ps.initCanState1(), ps.initCanState2()};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    for (uint32_t j = 0; j < PANDA_CAN_CNT; j++) {
							 | 
						||
| 
								 | 
							
								      const auto &can_health = pandaCanStates[i][j];
							 | 
						||
| 
								 | 
							
								      cs[j].setBusOff((bool)can_health.bus_off);
							 | 
						||
| 
								 | 
							
								      cs[j].setBusOffCnt(can_health.bus_off_cnt);
							 | 
						||
| 
								 | 
							
								      cs[j].setErrorWarning((bool)can_health.error_warning);
							 | 
						||
| 
								 | 
							
								      cs[j].setErrorPassive((bool)can_health.error_passive);
							 | 
						||
| 
								 | 
							
								      cs[j].setLastError(cereal::PandaState::PandaCanState::LecErrorCode(can_health.last_error));
							 | 
						||
| 
								 | 
							
								      cs[j].setLastStoredError(cereal::PandaState::PandaCanState::LecErrorCode(can_health.last_stored_error));
							 | 
						||
| 
								 | 
							
								      cs[j].setLastDataError(cereal::PandaState::PandaCanState::LecErrorCode(can_health.last_data_error));
							 | 
						||
| 
								 | 
							
								      cs[j].setLastDataStoredError(cereal::PandaState::PandaCanState::LecErrorCode(can_health.last_data_stored_error));
							 | 
						||
| 
								 | 
							
								      cs[j].setReceiveErrorCnt(can_health.receive_error_cnt);
							 | 
						||
| 
								 | 
							
								      cs[j].setTransmitErrorCnt(can_health.transmit_error_cnt);
							 | 
						||
| 
								 | 
							
								      cs[j].setTotalErrorCnt(can_health.total_error_cnt);
							 | 
						||
| 
								 | 
							
								      cs[j].setTotalTxLostCnt(can_health.total_tx_lost_cnt);
							 | 
						||
| 
								 | 
							
								      cs[j].setTotalRxLostCnt(can_health.total_rx_lost_cnt);
							 | 
						||
| 
								 | 
							
								      cs[j].setTotalTxCnt(can_health.total_tx_cnt);
							 | 
						||
| 
								 | 
							
								      cs[j].setTotalRxCnt(can_health.total_rx_cnt);
							 | 
						||
| 
								 | 
							
								      cs[j].setTotalFwdCnt(can_health.total_fwd_cnt);
							 | 
						||
| 
								 | 
							
								      cs[j].setCanSpeed(can_health.can_speed);
							 | 
						||
| 
								 | 
							
								      cs[j].setCanDataSpeed(can_health.can_data_speed);
							 | 
						||
| 
								 | 
							
								      cs[j].setCanfdEnabled(can_health.canfd_enabled);
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								      cs[j].setBrsEnabled(can_health.brs_enabled);
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								      cs[j].setCanfdNonIso(can_health.canfd_non_iso);
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      cs[j].setIrq0CallRate(can_health.irq0_call_rate);
							 | 
						||
| 
								 | 
							
								      cs[j].setIrq1CallRate(can_health.irq1_call_rate);
							 | 
						||
| 
								 | 
							
								      cs[j].setIrq2CallRate(can_health.irq2_call_rate);
							 | 
						||
| 
								 | 
							
								      cs[j].setCanCoreResetCnt(can_health.can_core_reset_cnt);
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    // Convert faults bitset to capnp list
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    std::bitset<sizeof(health.faults_pkt) * 8> fault_bits(health.faults_pkt);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    auto faults = ps.initFaults(fault_bits.count());
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    size_t j = 0;
							 | 
						||
| 
								 | 
							
								    for (size_t f = size_t(cereal::PandaState::FaultType::RELAY_MALFUNCTION);
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								         f <= size_t(cereal::PandaState::FaultType::HEARTBEAT_LOOP_WATCHDOG); f++) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      if (fault_bits.test(f)) {
							 | 
						||
| 
								 | 
							
								        faults.set(j, cereal::PandaState::FaultType(f));
							 | 
						||
| 
								 | 
							
								        j++;
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  pm->send("pandaStates", msg);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  return ignition_local;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								void send_peripheral_state(PubMaster *pm, Panda *panda) {
							 | 
						||
| 
								 | 
							
								  // build msg
							 | 
						||
| 
								 | 
							
								  MessageBuilder msg;
							 | 
						||
| 
								 | 
							
								  auto evt = msg.initEvent();
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  evt.setValid(panda->comms_healthy());
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  auto ps = evt.initPeripheralState();
							 | 
						||
| 
								 | 
							
								  ps.setPandaType(panda->hw_type);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  double read_time = millis_since_boot();
							 | 
						||
| 
								 | 
							
								  ps.setVoltage(Hardware::get_voltage());
							 | 
						||
| 
								 | 
							
								  ps.setCurrent(Hardware::get_current());
							 | 
						||
| 
								 | 
							
								  read_time = millis_since_boot() - read_time;
							 | 
						||
| 
								 | 
							
								  if (read_time > 50) {
							 | 
						||
| 
								 | 
							
								    LOGW("reading hwmon took %lfms", read_time);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  uint16_t fan_speed_rpm = panda->get_fan_speed();
							 | 
						||
| 
								 | 
							
								  ps.setFanSpeedRpm(fan_speed_rpm);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  pm->send("peripheralState", msg);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								void panda_state_thread(std::vector<Panda *> pandas, bool spoofing_started) {
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  util::set_thread_name("pandad_panda_state");
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  Params params;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  SubMaster sm({"controlsState"});
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  PubMaster pm({"pandaStates", "peripheralState"});
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  Panda *peripheral_panda = pandas[0];
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  bool is_onroad = false;
							 | 
						||
| 
								 | 
							
								  bool is_onroad_last = false;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  std::future<bool> safety_future;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  std::vector<std::string> connected_serials;
							 | 
						||
| 
								 | 
							
								  for (Panda *p : pandas) {
							 | 
						||
| 
								 | 
							
								    connected_serials.push_back(p->hw_serial());
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  LOGD("start panda state thread");
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  // run at 10hz
							 | 
						||
| 
								 | 
							
								  RateKeeper rk("panda_state_thread", 10);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  while (!do_exit && check_all_connected(pandas)) {
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    // send out peripheralState at 2Hz
							 | 
						||
| 
								 | 
							
								    if (sm.frame % 5 == 0) {
							 | 
						||
| 
								 | 
							
								      send_peripheral_state(&pm, peripheral_panda);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    auto ignition_opt = send_panda_states(&pm, pandas, spoofing_started);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (!ignition_opt) {
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      LOGE("Failed to get ignition_opt");
							 | 
						||
| 
								 | 
							
								      rk.keepTime();
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      continue;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    ignition = *ignition_opt;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    // check if we should have pandad reconnect
							 | 
						||
| 
								 | 
							
								    if (!ignition) {
							 | 
						||
| 
								 | 
							
								      bool comms_healthy = true;
							 | 
						||
| 
								 | 
							
								      for (const auto &panda : pandas) {
							 | 
						||
| 
								 | 
							
								        comms_healthy &= panda->comms_healthy();
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      if (!comms_healthy) {
							 | 
						||
| 
								 | 
							
								        LOGE("Reconnecting, communication to pandas not healthy");
							 | 
						||
| 
								 | 
							
								        do_exit = true;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      } else {
							 | 
						||
| 
								 | 
							
								        // check for new pandas
							 | 
						||
| 
								 | 
							
								        for (std::string &s : Panda::list(true)) {
							 | 
						||
| 
								 | 
							
								          if (!std::count(connected_serials.begin(), connected_serials.end(), s)) {
							 | 
						||
| 
								 | 
							
								            LOGW("Reconnecting to new panda: %s", s.c_str());
							 | 
						||
| 
								 | 
							
								            do_exit = true;
							 | 
						||
| 
								 | 
							
								            break;
							 | 
						||
| 
								 | 
							
								          }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								      }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      if (do_exit) {
							 | 
						||
| 
								 | 
							
								        break;
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    is_onroad = params.getBool("IsOnroad");
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // set new safety on onroad transition, after params are cleared
							 | 
						||
| 
								 | 
							
								    if (is_onroad && !is_onroad_last) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      if (!safety_future.valid() || safety_future.wait_for(0ms) == std::future_status::ready) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        safety_future = std::async(std::launch::async, safety_setter_thread, pandas);
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      } else {
							 | 
						||
| 
								 | 
							
								        LOGW("Safety setter thread already running");
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    is_onroad_last = is_onroad;
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    sm.update(0);
							 | 
						||
| 
								 | 
							
								    const bool engaged = sm.allAliveAndValid({"controlsState"}) && sm["controlsState"].getControlsState().getEnabled();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    for (const auto &panda : pandas) {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      panda->send_heartbeat(engaged);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    rk.keepTime();
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								void peripheral_control_thread(Panda *panda, bool no_fan_control) {
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  util::set_thread_name("pandad_peripheral_control");
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  SubMaster sm({"deviceState", "driverCameraState"});
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  uint64_t last_driver_camera_t = 0;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  uint16_t prev_fan_speed = 999;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  uint16_t ir_pwr = 0;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  uint16_t prev_ir_pwr = 999;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  FirstOrderFilter integ_lines_filter(0, 30.0, 0.05);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  while (!do_exit && panda->connected()) {
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    sm.update(1000);
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    if (sm.updated("deviceState") && !no_fan_control) {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      // Fan speed
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      uint16_t fan_speed = sm["deviceState"].getDeviceState().getFanSpeedPercentDesired();
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      if (fan_speed != prev_fan_speed || sm.frame % 100 == 0) {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        panda->set_fan_speed(fan_speed);
							 | 
						||
| 
								 | 
							
								        prev_fan_speed = fan_speed;
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if (sm.updated("driverCameraState")) {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      auto event = sm["driverCameraState"];
							 | 
						||
| 
								 | 
							
								      int cur_integ_lines = event.getDriverCameraState().getIntegLines();
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								      cur_integ_lines = integ_lines_filter.update(cur_integ_lines);
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      last_driver_camera_t = event.getLogMonoTime();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      if (cur_integ_lines <= CUTOFF_IL) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        ir_pwr = 100.0 * MIN_IR_POWER;
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      } else if (cur_integ_lines > SATURATE_IL) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        ir_pwr = 100.0 * MAX_IR_POWER;
							 | 
						||
| 
								 | 
							
								      } else {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        ir_pwr = 100.0 * (MIN_IR_POWER + ((cur_integ_lines - CUTOFF_IL) * (MAX_IR_POWER - MIN_IR_POWER) / (SATURATE_IL - CUTOFF_IL)));
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    // Disable IR on input timeout
							 | 
						||
| 
								 | 
							
								    if (nanos_since_boot() - last_driver_camera_t > 1e9) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      ir_pwr = 0;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    if (ir_pwr != prev_ir_pwr || sm.frame % 100 == 0 || ir_pwr >= 50.0) {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      panda->set_ir_pwr(ir_pwr);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      prev_ir_pwr = ir_pwr;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								void pandad_main_thread(std::vector<std::string> serials) {
							 | 
						||
| 
								 | 
							
								  LOGW("launching pandad");
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  if (serials.size() == 0) {
							 | 
						||
| 
								 | 
							
								    serials = Panda::list();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (serials.size() == 0) {
							 | 
						||
| 
								 | 
							
								      LOGW("no pandas found, exiting");
							 | 
						||
| 
								 | 
							
								      return;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  std::string serials_str;
							 | 
						||
| 
								 | 
							
								  for (int i = 0; i < serials.size(); i++) {
							 | 
						||
| 
								 | 
							
								    serials_str += serials[i];
							 | 
						||
| 
								 | 
							
								    if (i < serials.size() - 1) serials_str += ", ";
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  LOGW("connecting to pandas: %s", serials_str.c_str());
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  // connect to all provided serials
							 | 
						||
| 
								 | 
							
								  std::vector<Panda *> pandas;
							 | 
						||
| 
								 | 
							
								  for (int i = 0; i < serials.size() && !do_exit; /**/) {
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    Panda *p = connect(serials[i], i);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if (!p) {
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      util::sleep_for(100);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      continue;
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    pandas.push_back(p);
							 | 
						||
| 
								 | 
							
								    ++i;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  if (!do_exit) {
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    LOGW("connected to all pandas");
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    std::vector<std::thread> threads;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    threads.emplace_back(panda_state_thread, pandas, getenv("STARTED") != nullptr);
							 | 
						||
| 
								 | 
							
								    threads.emplace_back(peripheral_control_thread, pandas[0], getenv("NO_FAN_CONTROL") != nullptr);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    threads.emplace_back(can_send_thread, pandas, getenv("FAKESEND") != nullptr);
							 | 
						||
| 
								 | 
							
								    threads.emplace_back(can_recv_thread, pandas);
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    for (auto &t : threads) t.join();
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  for (Panda *panda : pandas) {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    delete panda;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								}
							 |