You are here

Set and store Current limit via Canbus

4 posts / 0 new
Last post
matrixgti
Offline
Last seen: 9 months 4 weeks ago
Joined: 2018-05-30 12:19
Posts: 7
Set and store Current limit via Canbus

Hi all,

I'm setting up a CAN controller for a VESC 6.0 using an Atmega2560 , I'm able to control RPM, Duty, and Current ring and get back  all the info from Message 5 but is not clear form me ho to dynamically set the maximum motor current. Not intended as primary control, just the maximum current as in the FOC parameter.

Here is the portion of the code from comm_can.c

 

/**
 * Update current limits on VESC on CAN-bus.
 *
 * @param controller_id
 * ID of the VESC.
 *
 * @param store
 * Store parameters in emulated EEPROM (FLASH).
 *
 * @param min
 * Minimum current (negative value).
 *
 * @param max
 * Maximum current.
 */
void comm_can_conf_current_limits(uint8_t controller_id,
		bool store, float min, float max) {
	int32_t send_index = 0;
	uint8_t buffer[8];
	buffer_append_float32(buffer, min, 1e3, &send_index);
	buffer_append_float32(buffer, max, 1e3, &send_index);
	comm_can_transmit_eid(controller_id |
			((uint32_t)(store ? CAN_PACKET_CONF_STORE_CURRENT_LIMITS :
					CAN_PACKET_CONF_CURRENT_LIMITS) << 8), buffer, send_index);
}

 

Did someone can point me on how to send and store a maximum current value ?

Thanks

Flavio

Rbz
Offline
Last seen: 3 years 9 months ago
Joined: 2020-04-11 18:43
Posts: 2

Hi,

 

Did you figure this out?

 

Kind Regards

matrixgti
Offline
Last seen: 9 months 4 weeks ago
Joined: 2018-05-30 12:19
Posts: 7

 

HI, Yes I did it and it work.

I use this command to set the max motor current "on the fly" but you can use the same routine to save it permanently using the right Can command emumered in the last code snip.

void comm_can_set_max_current(uint8_t controller_id, int32_t c_min, int32_t c_max) {
        int32_t send_index = 0;
        uint8_t buffer[8];
        buffer_append_int64(buffer, (int32_t)c_min,(int32_t)c_max, &send_index);
                        
        CAN.sendMsgBuf(controller_id | ((uint32_t)CAN_PACKET_CONF_CURRENT_LIMITS << 8), 1, send_index, buffer);


}

this is the routine to append the two numbers (current_min and current_max) into a  int_64 to a message

void buffer_append_int64(uint8_t* buffer, int32_t number,int32_t number2, int32_t *index) {
        buffer[(*index)++] = number >> 24;
        buffer[(*index)++] = number >> 16;
        buffer[(*index)++] = number >> 8;
        buffer[(*index)++] = number;        
        buffer[(*index)++] = number2 >> 24;
        buffer[(*index)++] = number2 >> 16;
        buffer[(*index)++] = number2 >> 8;
        buffer[(*index)++] = number2;

invoked by a simple potentiometer that set the max current from 5 to 40 Amps  (5000 -> 40000) :

 

    max_current = map(poti_value,0,99,5000,40000);
    comm_can_set_max_current(1,0,max_current);
    last_poti_value = poti_value;

This is the complete Enum commands usable in the Can protocol

// CAN commands
typedef enum {
  CAN_PACKET_SET_DUTY = 0,
  CAN_PACKET_SET_CURRENT,
  CAN_PACKET_SET_CURRENT_BRAKE,
  CAN_PACKET_SET_RPM,
  CAN_PACKET_SET_POS,
  CAN_PACKET_FILL_RX_BUFFER,
  CAN_PACKET_FILL_RX_BUFFER_LONG,
  CAN_PACKET_PROCESS_RX_BUFFER,
  CAN_PACKET_PROCESS_SHORT_BUFFER,
  CAN_PACKET_STATUS,
  CAN_PACKET_SET_CURRENT_REL,
  CAN_PACKET_SET_CURRENT_BRAKE_REL,
  CAN_PACKET_SET_CURRENT_HANDBRAKE,
  CAN_PACKET_SET_CURRENT_HANDBRAKE_REL,
  CAN_PACKET_STATUS_2,
  CAN_PACKET_STATUS_3,
  CAN_PACKET_STATUS_4,
  CAN_PACKET_PING,
  CAN_PACKET_PONG,
  CAN_PACKET_DETECT_APPLY_ALL_FOC,
  CAN_PACKET_DETECT_APPLY_ALL_FOC_RES,
  CAN_PACKET_CONF_CURRENT_LIMITS,
  CAN_PACKET_CONF_STORE_CURRENT_LIMITS,
  CAN_PACKET_CONF_CURRENT_LIMITS_IN,
  CAN_PACKET_CONF_STORE_CURRENT_LIMITS_IN,
  CAN_PACKET_CONF_FOC_ERPMS,
  CAN_PACKET_CONF_STORE_FOC_ERPMS,
  CAN_PACKET_STATUS_5
} CAN_PACKET_ID;

Hope this help and sorry for my bad english,

 

Cheers

 

Flavio

Rbz
Offline
Last seen: 3 years 9 months ago
Joined: 2020-04-11 18:43
Posts: 2

Thank you, that worked out for me as well, much appreciated.