/* * AP_VOLZ_PROTOCOL.cpp * * Created on: Oct 31, 2017 * Author: guy */ #include "AP_Volz_Protocol.h" #if AP_VOLZ_ENABLED #include #include #include extern const AP_HAL::HAL& hal; const AP_Param::GroupInfo AP_Volz_Protocol::var_info[] = { // @Param: MASK // @DisplayName: Channel Bitmask // @Description: Enable of volz servo protocol to specific channels // @Bitmask: 0:Channel1,1:Channel2,2:Channel3,3:Channel4,4:Channel5,5:Channel6,6:Channel7,7:Channel8,8:Channel9,9:Channel10,10:Channel11,11:Channel12,12:Channel13,13:Channel14,14:Channel15,15:Channel16,16:Channel17,17:Channel18,18:Channel19,19:Channel20,20:Channel21,21:Channel22,22:Channel23,23:Channel24,24:Channel25,25:Channel26,26:Channel27,28:Channel29,29:Channel30,30:Channel31,31:Channel32 // @User: Standard AP_GROUPINFO("MASK", 1, AP_Volz_Protocol, bitmask, 0), AP_GROUPEND }; // constructor AP_Volz_Protocol::AP_Volz_Protocol(void) { // set defaults from the parameter table AP_Param::setup_object_defaults(this, var_info); } void AP_Volz_Protocol::init(void) { AP_SerialManager &serial_manager = AP::serialmanager(); port = serial_manager.find_serial(AP_SerialManager::SerialProtocol_Volz,0); update_volz_bitmask(bitmask); } void AP_Volz_Protocol::update() { if (!initialised) { initialised = true; init(); } if (port == nullptr) { return; } if (last_used_bitmask != uint32_t(bitmask.get())) { update_volz_bitmask(bitmask); } uint32_t now = AP_HAL::micros(); if (((now - last_volz_update_time) < volz_time_frame_micros) || (port->txspace() < sizeof(CMD))) { return; } last_volz_update_time = now; uint8_t i; uint16_t value; // loop for all channels for (i=0; iget_output_pwm() < VOLZ_PWM_POSITION_MIN) { value = 0; } else { value = c->get_output_pwm() - VOLZ_PWM_POSITION_MIN; } // scale the PWM value to Volz value value = value * VOLZ_SCALE_VALUE / (VOLZ_PWM_POSITION_MAX - VOLZ_PWM_POSITION_MIN); value = value + VOLZ_EXTENDED_POSITION_MIN; // make sure value stays in range if (value > VOLZ_EXTENDED_POSITION_MAX) { value = VOLZ_EXTENDED_POSITION_MAX; } // prepare Volz protocol data. CMD cmd; cmd.ID = SET_EXTENDED_POSITION_CMD; cmd.actuator_id = i + 1; // send actuator id as 1 based index so ch1 will have id 1, ch2 will have id 2 .... cmd.arg1 = HIGHBYTE(value); cmd.arg2 = LOWBYTE(value); send_command(cmd); } } } // calculate CRC for volz serial protocol and send the data. void AP_Volz_Protocol::send_command(CMD &cmd) { uint16_t crc = 0xFFFF; // calculate Volz CRC value according to protocol definition for(uint8_t i=0; i<4; i++) { // take input data into message that will be transmitted. crc = (cmd.data[i] << 8) ^ crc; for(uint8_t j=0; j<8; j++) { if (crc & 0x8000) { crc = (crc << 1) ^ 0x8005; } else { crc = crc << 1; } } } // add CRC result to the message cmd.crc1 = HIGHBYTE(crc); cmd.crc2 = LOWBYTE(crc); port->write(cmd.data, sizeof(cmd)); } void AP_Volz_Protocol::update_volz_bitmask(uint32_t new_bitmask) { uint8_t count = 0; last_used_bitmask = new_bitmask; for (uint8_t i=0; i