libpandac  0.0.0
A library designed for a comm.ai Panda.
toyota.h
1 /*
2  Author: Matt Bunting
3  Copyright (c) 2020 Arizona Board of Regents
4  All rights reserved.
5 
6  Permission is hereby granted, without written agreement and without
7  license or royalty fees, to use, copy, modify, and distribute this
8  software and its documentation for any purpose, provided that the
9  above copyright notice and the following two paragraphs appear in
10  all copies of this software.
11 
12  IN NO EVENT SHALL THE ARIZONA BOARD OF REGENTS BE LIABLE TO ANY PARTY
13  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
16  SUCH DAMAGE.
17 
18  THE ARIZONA BOARD OF REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER
21  IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION
22  TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 
24  */
25 
26 #ifndef TOYOTA_H
27 #define TOYOTA_H
28 
29 
30 #include "mogi/thread.h"
31 
32 #include "panda.h"
33 
34 #define TIME_HEARTBEAT_FAIL_STEERING (1.0) // In seconds, time until a heartbeat fails from not receiving a new steering command
35 #define TIME_HEARTBEAT_FAIL_ACCELERATION (1.0) // In seconds, time until a heartbeat fails from not receiving a new acceleration command
36 
37 #define TOYOTA_COMMAND_THREAD_RATE (600.0) // Defines the rate of the thread, not for any particular command to be sent.
38 #define TOYOTA_RATE_HEARTBEAT (1.0) // This is for the panda in general, not Toyota specific
39 #define TOYOTA_RATE_LKA (1.0) // Rate of the LKAS_HUD command
40 #define TOYOTA_RATE_TRACK_B (40.0) // Rate of the TRACK_B_1 command
41 #define TOYOTA_RATE_STEER (100.0) // Rate of the STEERING_LKA command
42 #define TOYOTA_RATE_ACC (30.0) // Rate of the ACC_CONTROL command
43 
44 #define TOYOTA_DECIMATOR_MAX_HEARTBEAT (TOYOTA_COMMAND_THREAD_RATE/TOYOTA_RATE_HEARTBEAT)
45 #define TOYOTA_DECIMATOR_MAX_LKA (TOYOTA_COMMAND_THREAD_RATE/TOYOTA_RATE_LKA)
46 #define TOYOTA_DECIMATOR_MAX_TRACK_B (TOYOTA_COMMAND_THREAD_RATE/TOYOTA_RATE_TRACK_B)
47 #define TOYOTA_DECIMATOR_MAX_STEER (TOYOTA_COMMAND_THREAD_RATE/TOYOTA_RATE_STEER)
48 #define TOYOTA_DECIMATOR_MAX_ACC (TOYOTA_COMMAND_THREAD_RATE/TOYOTA_RATE_ACC)
49 
50 namespace Panda {
51 
62 CanFrame buildLkasHud(bool lkaAlert, unsigned char leftLane, unsigned char rightLane, bool barrier, bool twoBeeps, bool repeatedBeeps);
63 
72 CanFrame buildSteeringLKA( unsigned char count, int16_t steerTorque, bool steerRequest, unsigned char lkaState );
73 
82 CanFrame buildACC_CONTROL(double acc, bool permitBraking, bool releaseStandstill, bool miniCar, bool cancelRequest);
83 
90 CanFrame buildTRACK_B_1(unsigned char count);
91 
95 CanFrame buildPCM_CRUISE_2(unsigned char SET_SPEED);
96 
100 CanFrame buildDSU_CRUISE(unsigned char SET_SPEED);
101 
107 uint8_t toyotaChecksum(Panda::CanFrame& frame);
108 
113 void printFrame( Panda::CanFrame frame );
114 
121 class ToyotaHandler : public Mogi::Thread {
122 private:
123 
124  // Overloaded from Mogi::Thread.
125  // This will enable the required power save mode for vehicle control.
126  void entryAction();
127  void exitAction();
128 
129  // Overloaded from Mogi::Thread.
130  // This handles the constant updates.
131  void doAction();
132 
133  // All of the following are called from doAction()
134  void sendHeartBeat();
135  void sendLka();
136  void sendTrackB();
137  void sendSteer();
138  void sendAcc();
139 
140  // This will max the LKA decimator to trigger an instant send.
141  void triggerInstantLkaSend();
142 
143  // Helper functions to check whether the hearbeats currently pass.
144  // These will pass if a command was sent within the corresponding defined times:
145  // TIME_HEARTBEAT_FAIL_STEERING
146  // TIME_HEARTBEAT_FAIL_ACCELERATION
147  bool heartbeatSteeringPass();
148  bool heartbeatAccelerationPass();
149 
150  Panda::Handler* pandaHandler;
151  PandaHealth health;
152 
153  int decimatorHeartbeat;
154  int decimatorLka;
155  int decimatorTrackB;
156  int decimatorSteer;
157  int decimatorAcc;
158 
159  // For building proper CanFrames for steering and track B:
160  unsigned char counterSteer;
161  unsigned char counterTrackB;
162 
163  // HUD:
164  bool hudLdaAlert;
165  bool hudBarrier;
166  unsigned char hudLeftLane;
167  unsigned char hudRightLane;
168  bool hudTwoBeeps;
169  bool hudRepeatedBeeps;
170 
171  // Steering Control:
172  double steerTorqueControl;
173  bool steerRequest;
174  unsigned char steerLkaState;
175 
176  // Acceleration Control:
177  double accelerationControl;
178  bool permitBraking;
179  bool releaseStandstill;
180  bool miniCar;
181  bool cancelRequest;
182 
183  // Safety management, only send control commands if they are continuously updated
184  int heartBeatSteer;
185  int heartBeatAcceleration;
186 
187 public:
188 
193  ToyotaHandler(Panda::Handler* handler);
194 
199  void setHudLdaAlert( bool enable );
200 
205  void setHudBarrier( bool enable );
206 
217  void setHudLanes( unsigned char laneLeft, unsigned char laneRight );
218 
223  void setHudTwoBeeps( bool enable );
224 
230  void setHudRepeatedBeeps( bool enable );
231 
239  void setHudMiniCar( bool enable );
240 
246  void setHudCruiseCancelRequest( bool enable );
247 
256  void setSteerTorque( int steerTorque );
257 
276  void setAcceleration( double acceleration );
277 
286  bool getIgnitionOn();
287 
323  bool getControlsAllowed();
324 
329  const PandaHealth& getPandaHealth() const;
330 };
331 
332 }
333 
334 #endif
A class that handles the Usb, GPS, and CAN data.
Definition: panda.h:50
Handles USB communication and parsing with a comma.ai Panda.
Definition: can.h:38
void setHudMiniCar(bool enable)
Displays the "Mini Car" on the HUD.
A threaded interface class that handles sending contorl commands to a Panda via a Panda::Handler...
Definition: toyota.h:121
void setHudLanes(unsigned char laneLeft, unsigned char laneRight)
Shows different lanes for the right and left side. Valid values 0-3:
CanFrame buildDSU_CRUISE(unsigned char SET_SPEED)
Unused by ToyotaHandler. Also untested.
void printFrame(Panda::CanFrame frame)
This funciton is helpful for debugging but should either not live in toyota.h or shoul dbe removed: ...
void setSteerTorque(int steerTorque)
Sends a steering torque to the steering wheel (non-working).
Abstract class, handles a single thread. Features mutual exclusion and pause/resume.
Definition: thread.h:27
bool getIgnitionOn()
Returns the Panda report for whether the ignition is on (line).
CanFrame buildPCM_CRUISE_2(unsigned char SET_SPEED)
Unused by ToyotaHandler. Also untested.
ToyotaHandler(Panda::Handler *handler)
Construction must be done with a Panda::Handler.
Definition: candata.h:38
const PandaHealth & getPandaHealth() const
Returns the full Panda Health state. controls_allowed and ignition_line can be read form this...
void setHudBarrier(bool enable)
Shows lane barriers on the HUD.
CanFrame buildTRACK_B_1(unsigned char count)
Constructs the TRACK_B_1 command, needed to fake adaptive cruise controller operation. Note that there could be more things to fake in this command, but for use of ACC_CONTROL this command is intercepted to prevent run-time faults of the ACC.
void setHudRepeatedBeeps(bool enable)
Will cause continuous beeping.
void setAcceleration(double acceleration)
Sends acceleration to the cruise controller, in units of m/s^2.
CanFrame buildLkasHud(bool lkaAlert, unsigned char leftLane, unsigned char rightLane, bool barrier, bool twoBeeps, bool repeatedBeeps)
Constructs the LKAS_HUS command that works on a Toyota RAV4.
CanFrame buildACC_CONTROL(double acc, bool permitBraking, bool releaseStandstill, bool miniCar, bool cancelRequest)
Constructs the ACC_CONTROL command, used for sending cruise cntrol accelerations. ...
void setHudTwoBeeps(bool enable)
Will cause a double-beep alert. Appears to be inconsistent in operation.
CanFrame buildSteeringLKA(unsigned char count, int16_t steerTorque, bool steerRequest, unsigned char lkaState)
Constructs the STEERING_LKA command, used for sending steering torque.
void setHudLdaAlert(bool enable)
Tells the driver to grab the steering wheel.
uint8_t toyotaChecksum(Panda::CanFrame &frame)
Computes particular checksums within the CAN message, and is not the CRC for the CAN frame itself...
A CAN bus packet data.
bool getControlsAllowed()
Returns the Panda health report for whether controls are allowed. Controls are allowed when the cruis...
void setHudCruiseCancelRequest(bool enable)
Invokes a cruise control cancel request, for the driver to take control over vehicle. This will disable cruise control. Commands can only be sent to the car again if the driver re-activates cruise control.