Program Listing for File ref_serial.hpp

Return to documentation for file (src/tap/communication/serial/ref_serial.hpp)

/*
 * Copyright (c) 2020-2021 Advanced Robotics at the University of Washington <robomstr@uw.edu>
 *
 * This file is part of Taproot.
 *
 * Taproot is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Taproot is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Taproot.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef TAPROOT_REF_SERIAL_HPP_
#define TAPROOT_REF_SERIAL_HPP_

#include <cmath>
#include <cstdint>
#include <unordered_map>

#include "tap/architecture/timeout.hpp"
#include "tap/util_macros.hpp"

#include "modm/container/deque.hpp"
#include "modm/processing/protothread/semaphore.hpp"

#include "dji_serial.hpp"
#include "ref_serial_data.hpp"

namespace tap
{
class Drivers;
}

namespace tap::communication::serial
{
class RefSerial : public DJISerial, public RefSerialData
{
private:
    static constexpr uint32_t TIME_OFFLINE_REF_DATA_MS = 1000;

    // RX message constants
    static constexpr uint16_t DPS_TRACKER_DEQUE_SIZE = 20;

public:
    enum MessageType
    {
        REF_MESSAGE_TYPE_GAME_STATUS = 0x1,
        REF_MESSAGE_TYPE_GAME_RESULT = 0x2,
        REF_MESSAGE_TYPE_ALL_ROBOT_HP = 0x3,

        REF_MESSAGE_TYPE_SITE_EVENT_DATA = 0x101,
        REF_MESSAGE_TYPE_PROJECTILE_SUPPPLIER_ACTION = 0x102,
        REF_MESSAGE_TYPE_WARNING_DATA = 0x104,
        REF_MESSAGE_TYPE_DART_INFO = 0x105,

        REF_MESSAGE_TYPE_ROBOT_STATUS = 0x201,
        REF_MESSAGE_TYPE_POWER_AND_HEAT = 0x202,
        REF_MESSAGE_TYPE_ROBOT_POSITION = 0x203,
        REF_MESSAGE_TYPE_ROBOT_BUFF_STATUS = 0x204,
        REF_MESSAGE_TYPE_AERIAL_ENERGY_STATUS = 0x205,
        REF_MESSAGE_TYPE_RECEIVE_DAMAGE = 0x206,
        REF_MESSAGE_TYPE_PROJECTILE_LAUNCH = 0x207,
        REF_MESSAGE_TYPE_BULLETS_REMAIN = 0x208,
        REF_MESSAGE_TYPE_RFID_STATUS = 0x209,
        REF_MESSAGE_TYPE_DART_STATION_INFO = 0x20A,
        REF_MESSAGE_TYPE_GROUND_ROBOT_POSITION = 0x20B,
        REF_MESSAGE_TYPE_RADAR_PROGRESS = 0x20C,
        REF_MESSAGE_TYPE_SENTRY_INFO = 0x20D,
        REF_MESSAGE_TYPE_RADAR_INFO = 0x20E,

        REF_MESSAGE_TYPE_CUSTOM_DATA = 0x301,
        // REF_MESSAGE_TYPE_CUSTOM_CONTROLLER = 0x302,
        // REF_MESSAGE_TYPE_SMALL_MAP = 0x303,
        // REF_MESSAGE_TYPE_VTM_INPUT_DATA = 0x304
    };

    RefSerial(Drivers* drivers);
    DISALLOW_COPY_AND_ASSIGN(RefSerial)
    mockable ~RefSerial() = default;

    void messageReceiveCallback(const ReceivedSerialMessage& completeMessage) override;

    mockable bool getRefSerialReceivingData() const;

    mockable const Rx::RobotData& getRobotData() const;

    mockable const Rx::GameData& getGameData() const;

    mockable RobotId getRobotIdBasedOnCurrentRobotTeam(RobotId id);

    mockable void attachRobotToRobotMessageHandler(
        uint16_t msgId,
        RobotToRobotMessageHandler* handler);

    mockable bool acquireTransmissionSemaphore()
    {
        if (transmissionDelayTimer.isExpired() || transmissionDelayTimer.isStopped())
        {
            return transmissionSemaphore.acquire();
        }
        return false;
    }

    mockable void releaseTransmissionSemaphore(uint32_t sentMsgLen)
    {
        transmissionSemaphore.release();
        transmissionDelayTimer.restart(
            std::ceil(sentMsgLen * 1000.0f / Tx::MAX_TRANSMIT_SPEED_BYTES_PER_S));
    }

    bool operatorBlinded() const;

    static inline bool heatAndLimitValid(uint16_t heat, uint16_t heatLimit)
    {
        return heat != 0xffff && heatLimit != 0xffff && heatLimit != 0;
    }

private:
    Rx::RobotData robotData;
    Rx::GameData gameData;
    modm::BoundedDeque<Rx::DamageEvent, DPS_TRACKER_DEQUE_SIZE> receivedDpsTracker;
    arch::MilliTimeout refSerialOfflineTimeout;
    std::unordered_map<uint16_t, RobotToRobotMessageHandler*> msgIdToRobotToRobotHandlerMap;
    modm::pt::Semaphore transmissionSemaphore;
    tap::arch::MilliTimeout transmissionDelayTimer;

    bool decodeToGameStatus(const ReceivedSerialMessage& message);
    bool decodeToGameResult(const ReceivedSerialMessage& message);
    bool decodeToAllRobotHP(const ReceivedSerialMessage& message);
    bool decodeToSiteEventData(const ReceivedSerialMessage& message);
    bool decodeToProjectileSupplierAction(const ReceivedSerialMessage& message);
    bool decodeToWarningData(const ReceivedSerialMessage& message);
    bool decodeToDartInfo(const ReceivedSerialMessage& message);
    bool decodeToRobotStatus(const ReceivedSerialMessage& message);
    bool decodeToPowerAndHeat(const ReceivedSerialMessage& message);
    bool decodeToRobotPosition(const ReceivedSerialMessage& message);
    bool decodeToRobotBuffs(const ReceivedSerialMessage& message);
    bool decodeToAerialEnergyStatus(const ReceivedSerialMessage& message);
    bool decodeToDamageStatus(const ReceivedSerialMessage& message);
    bool decodeToProjectileLaunch(const ReceivedSerialMessage& message);
    bool decodeToBulletsRemain(const ReceivedSerialMessage& message);
    bool decodeToRFIDStatus(const ReceivedSerialMessage& message);
    bool decodeToDartStation(const ReceivedSerialMessage& message);
    bool decodeToGroundPositions(const ReceivedSerialMessage& message);
    bool decodeToRadarProgress(const ReceivedSerialMessage& message);
    bool decodeToSentryInfo(const ReceivedSerialMessage& message);
    bool decodeToRadarInfo(const ReceivedSerialMessage& message);

    bool handleRobotToRobotCommunication(const ReceivedSerialMessage& message);

    void updateReceivedDamage();
    void processReceivedDamage(uint32_t timestamp, int32_t damageTaken);
};

}  // namespace tap::communication::serial

#endif  // TAPROOT_REF_SERIAL_HPP_