|
| 1 | +/* |
| 2 | + * RmdMotorCAN.h |
| 3 | + * |
| 4 | + * Created on: Dec 7, 2024 |
| 5 | + * Author: Yannick |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef USEREXTENSIONS_SRC_RMDMOTORCAN_H_ |
| 9 | +#define USEREXTENSIONS_SRC_RMDMOTORCAN_H_ |
| 10 | + |
| 11 | +#include "MotorDriver.h" |
| 12 | +#include "cpp_target_config.h" |
| 13 | +#include "CAN.h" |
| 14 | +#include "Encoder.h" |
| 15 | +#include "thread.hpp" |
| 16 | +#include "CanHandler.h" |
| 17 | +#include "CommandHandler.h" |
| 18 | +#include "PersistentStorage.h" |
| 19 | + |
| 20 | +#ifdef RMDCAN |
| 21 | +#define RMD_THREAD_MEM 256 |
| 22 | +#define RMD_THREAD_PRIO 25 // Must be higher than main thread |
| 23 | + |
| 24 | +enum class RmdCAN_commands : uint32_t{ |
| 25 | + canid,errors,maxtorque,current,temperature,voltage,activerequests,modelname |
| 26 | +}; |
| 27 | + |
| 28 | +class RmdMotorCAN : public MotorDriver,public PersistentStorage, public Encoder, public CanHandler, public CommandHandler, cpp_freertos::Thread { |
| 29 | +public: |
| 30 | + RmdMotorCAN(uint8_t instance); |
| 31 | + virtual ~RmdMotorCAN(); |
| 32 | + |
| 33 | + const ClassIdentifier getInfo() = 0; |
| 34 | + void turn(int16_t power) override; |
| 35 | + void stopMotor() override; |
| 36 | + void startMotor() override; |
| 37 | + Encoder* getEncoder() override; |
| 38 | + bool hasIntegratedEncoder() {return true;} |
| 39 | + void setTorque(int16_t torque); |
| 40 | + bool motorReady(); |
| 41 | + |
| 42 | + void Run(); |
| 43 | + |
| 44 | + void saveFlash() override; // Write to flash here |
| 45 | + void restoreFlash() override; // Load from flash |
| 46 | + |
| 47 | +// float getPos_f() override; |
| 48 | + uint32_t getCpr() override; |
| 49 | + int32_t getPos() override; |
| 50 | + void setPos(int32_t pos) override; |
| 51 | + EncoderType getEncoderType() override; |
| 52 | + |
| 53 | + void setCanFilter(); |
| 54 | + void updateRequestMode(bool activerequests); |
| 55 | + |
| 56 | + void sendMsg(std::array<uint8_t,8> &data,uint8_t len = 8); |
| 57 | + void sendCmd(uint8_t cmd); |
| 58 | + |
| 59 | + void canRxPendCallback(CANPort* port,CAN_rx_msg& msg) override; |
| 60 | + void updateStatus(); |
| 61 | + |
| 62 | + void requestConstantReports(uint8_t cmd,bool enable,uint16_t interval_10ms); |
| 63 | + |
| 64 | + void registerCommands(); |
| 65 | + CommandStatus command(const ParsedCommand& cmd,std::vector<CommandReply>& replies) override; |
| 66 | + |
| 67 | + const uint32_t angleUpdateMs = 10; |
| 68 | + |
| 69 | + union ErrorStatus { |
| 70 | + struct ErrorStatur_s{ |
| 71 | + uint16_t res1 : 1, |
| 72 | + stall : 1, |
| 73 | + undervoltage : 1, |
| 74 | + overvoltage : 1, |
| 75 | + overcurrent : 1, |
| 76 | + overpower : 1, |
| 77 | + calibrationWriteErr : 1, |
| 78 | + overspeed : 1, |
| 79 | + overtemp : 1, |
| 80 | + calibrationErr : 1, |
| 81 | + res2 : 6; |
| 82 | + }; |
| 83 | + uint16_t asInt; |
| 84 | + ErrorStatur_s flags; |
| 85 | + }; |
| 86 | +private: |
| 87 | + CANPort* port = &canport; |
| 88 | + uint8_t nodeId = 1; // can ID 1-32 |
| 89 | + uint8_t motorId = 0; |
| 90 | + bool active = false; |
| 91 | + bool nextAvailable = false; |
| 92 | + bool available = false; |
| 93 | + bool activerequests = false; |
| 94 | + bool requestConstantReportEnable = false; |
| 95 | + |
| 96 | + char modelName[8] = {0}; |
| 97 | + |
| 98 | + uint32_t lastAngleUpdate = 0; |
| 99 | + uint32_t lastTorqueStatusUpdate_us = 0; |
| 100 | + |
| 101 | + float lastPos = 0; |
| 102 | + uint16_t maxTorque = 1000; // in 0.01A, max 0x7fff |
| 103 | + |
| 104 | + int32_t filterId = -2; |
| 105 | + |
| 106 | + int16_t curCurrent = 0; |
| 107 | + uint8_t curTemp = 0; |
| 108 | + uint16_t curVoltage = 0; |
| 109 | + |
| 110 | + ErrorStatus lastErrors = {0}; |
| 111 | + |
| 112 | + int32_t posOffset = 0; |
| 113 | + |
| 114 | + void errorCb(ErrorStatus &errors); |
| 115 | +}; |
| 116 | + |
| 117 | + |
| 118 | +/** |
| 119 | + * Instance 1 of RMD motor |
| 120 | + */ |
| 121 | +class RmdMotorCAN1 : public RmdMotorCAN{ |
| 122 | +public: |
| 123 | + RmdMotorCAN1() : RmdMotorCAN{0} {inUse = true;} |
| 124 | + const ClassIdentifier getInfo(); |
| 125 | + ~RmdMotorCAN1(){inUse = false;} |
| 126 | + static bool isCreatable(); |
| 127 | + static ClassIdentifier info; |
| 128 | + static bool inUse; |
| 129 | +}; |
| 130 | + |
| 131 | +/** |
| 132 | + * Instance 2 of RMD motor |
| 133 | + */ |
| 134 | +class RmdMotorCAN2 : public RmdMotorCAN{ |
| 135 | +public: |
| 136 | + RmdMotorCAN2() : RmdMotorCAN{1} {inUse = true;} |
| 137 | + const ClassIdentifier getInfo(); |
| 138 | + ~RmdMotorCAN2(){inUse = false;} |
| 139 | + static bool isCreatable(); |
| 140 | + static ClassIdentifier info; |
| 141 | + static bool inUse; |
| 142 | +}; |
| 143 | +#endif |
| 144 | +#endif /* USEREXTENSIONS_SRC_RMDMOTORCAN_H_ */ |
0 commit comments