You are here

Query regarding removing code that is not needed for my project

5 posts / 0 new
Last post
jackherer
Offline
Last seen: 8 months 3 weeks ago
VESC Free
Joined: 2021-03-30 19:20
Posts: 8
Query regarding removing code that is not needed for my project

I am wanting to remove all the code from the VESC firmware that I will never need. So I will never use any kind of servo, and so I don't need all the encoder code then??

I will only be using hall sensors or sensorless.

As I'm going through the code and removing everything related to servos and encoders I came across the mc_interface_get_pid_pos_now function in mc_interface.c:

float mc_interface_get_pid_pos_now(void) {
	float ret = 0.0;

	switch (motor_now()->m_conf.motor_type) {
		case MOTOR_TYPE_BLDC:
			ret = encoder_read_deg();
			break;

		case MOTOR_TYPE_FOC:
			ret = mcpwm_foc_get_pid_pos_now();
			break;

		default:
			break;
	}

	ret *= DIR_MULT;
	utils_norm_angle(&ret);

	return ret;
}

This leads to the encoder_read_deg function:

float encoder_read_deg(void) {
	static float angle = 0.0;

	switch (mode) {
	case ENCODER_MODE_ABI:
		angle = ((float)HW_ENC_TIM->CNT * 360.0) / (float)enc_counts;
		break;

	case ENCODER_MODE_AS5047P_SPI:
	case ENCODER_MODE_MT6816_SPI:
	case RESOLVER_MODE_AD2S1205:
	case ENCODER_MODE_TS5700N8501:
		angle = last_enc_angle;
		break;

#ifdef HW_HAS_SIN_COS_ENCODER
	case ENCODER_MODE_SINCOS: {
		float sin = ENCODER_SIN_VOLTS * sin_gain - sin_offset;
		float cos = ENCODER_COS_VOLTS * cos_gain - cos_offset;

		float module = SQ(sin) + SQ(cos);

		if (module > SQ(SINCOS_MAX_AMPLITUDE) )	{
			// signals vector outside of the valid area. Increase error count and discard measurement
			++sincos_signal_above_max_error_cnt;
			UTILS_LP_FAST(sincos_signal_above_max_error_rate, 1.0, 1./SINCOS_SAMPLE_RATE_HZ);
			angle = last_enc_angle;
		}
		else {
			if (module < SQ(SINCOS_MIN_AMPLITUDE)) {
				++sincos_signal_below_min_error_cnt;
				UTILS_LP_FAST(sincos_signal_low_error_rate, 1.0, 1./SINCOS_SAMPLE_RATE_HZ);
				angle = last_enc_angle;
			}
			else {
				UTILS_LP_FAST(sincos_signal_above_max_error_rate, 0.0, 1./SINCOS_SAMPLE_RATE_HZ);
				UTILS_LP_FAST(sincos_signal_low_error_rate, 0.0, 1./SINCOS_SAMPLE_RATE_HZ);

				float angle_tmp = utils_fast_atan2(sin, cos) * 180.0 / M_PI;
				UTILS_LP_FAST(angle, angle_tmp, sincos_filter_constant);
				last_enc_angle = angle;
			}
		}
		break;
	}
#endif

	default:
		break;
	}

	return angle;
}

Is this funtion only used for servo motors? Can I safely remove this function if I am only running a motor with hall sensors or no sensors at all?

 

TechAUmNu
Offline
Last seen: 1 day 16 hours ago
VESC Free
Joined: 2017-09-22 01:27
Posts: 575

Erm.. why do you want to remove all the code you don't use? Are you trying to certify the firmware or something?

jackherer
Offline
Last seen: 8 months 3 weeks ago
VESC Free
Joined: 2021-03-30 19:20
Posts: 8

Because why have many thousands of lines of code in the firmware that will never, ever get used...!? Isn't it obvs? The firmware compiles to around 400kb, I can reduce thaqt by at least a quarter, which means I can introduce further custom functionality. 

Is there any point having the functionality of a servo on an ebike? No, So i dont want that code. Ill never use the WS2811 LEDs either, or an encoder... or the DRV8xxx. 

Theres a lot of code i will never use in my application, I am a computer programmer and an electrical engineer.. Its literally what I do everyday. Its fun, its a learning project. Im really getting to know the code now too.

 

JamieJackHerer

CTSchorsch
Offline
Last seen: 3 months 4 weeks ago
VESC Free
Joined: 2018-07-13 09:55
Posts: 101

The firmware is already very configurable. Maybe you don't delete any code, but add more #defines and #ifdefs, so so code base can be the same

jackherer
Offline
Last seen: 8 months 3 weeks ago
VESC Free
Joined: 2021-03-30 19:20
Posts: 8

Add more code? As I say I am NEVER going to use some of the code, I know I can use compile options and defines but simply put, this is the way I like to do things.. and by doing this I'm learning the code right in the deep end. 

Id like to have a fully customised VESC firmware. I can't see why anyone would have an issue with it, it's open source and I'm sure Benjamin made it all open source so that people like me can fully customise their own software and hardware.

I will eventually work out what everything does in the code, but obvs it would be much more helpful and friendly for folk on this forum to help me out and speed up my development. 

I do plan to build a crazy powerful build based on the VESC 6 HW, looking at something like a 250V 1000A, 3AWG phase and battery wires, and hopefully an upgrade to the WiFi powered STM32Wx MCU. Otherwise it's gonna be using another MCU like the ESP8266x. I have the schematics for the esp-12f and the ESP32 WROOM boards, so could incorporate the circuit into my board, but I'd rather use the same STM32 for BLE/WiFi. 

 

Possibly looking to build a python lib too, similar to pyvesc, but with a lot of async usage and an upto date code flow. 

JamieJackHerer