Code covered by the BSD License  

Highlights from
NXT Ballbot (Self-Balancing Robot On A Ball) Controller Design

image thumbnail

NXT Ballbot (Self-Balancing Robot On A Ball) Controller Design

by

 

NXT Ballbot Model-Based Design based on Embedded Coder Robot NXT.

pre_sdo_codegen.m
% Pre-Processing of Code Generation with Simulink Data Object
% Make signal and parameter objects to define variable specifications (RAM/ROM)
% Resolve signal resolution with DISABLEIMPLICITSIGNALRESOLUTION

% make signal objects
sig.name = { ...
	'timer_stop0', 'bluetooth_rx', ...						% signals
	'flag_mode', 'flag_auto', 'flag_avoid', ...				% states (data store memories)
	'operating_state', 'battery', 'gyro_offset', ...
	'theta_diff', 'psi_diff', 'start_time', ...
	'ud_int_psidot', 'ud_int_theta_diff', ...				% states (unit delay blocks)
	'ud_int_thetadot_ref', 'ud_drv_theta', ...
	'ud_lpf_theta', 'ud_lpf_thetadot_ref', ...
	'ud_detect_increse', 'ud_flag_log' ...
	};
for n = 1:length(sig.name)
	cmd = strcat( ...
		sig.name{n}, ' = ', 'mpt.Signal;', ...
		sig.name{n}, '.RTWInfo.CustomAttributes.DefinitionFile = ''ballbot_ram.c'';', ...
		sig.name{n}, '.RTWInfo.CustomAttributes.HeaderFile = ''ballbot_ram.h'';' ...
		);
	eval(cmd)
end

% resolve signal resolution
sig.handle = disableimplicitsignalresolution(bdroot(gcb));

% make parameter objects
param{1}.name = { ...
	'k_f', 'k_i', 'k_thetadot', 'ts1', 'ts2', 'ts3', ...
	'time_start', 'time_auto', 'time_stop', ...
	'calib', 'control', 'stop', 'pwm_gain', 'pwm_offset', ...
	'a_b', 'a_d', 'a_r', 'a_gc', 'a_gd', ...
	'dst_thr', 'psi_diff_thr', 'theta_diff_thr', ...
	'sound_frq', 'sound_dur', 'gp_max', 'log_count', 'lsb', ...
	};
param{2}.name = {'Rs', 'Rw'};
for m = 1:length(param)
	param{m}.num = length(param{m}.name);
	param{m}.value = cell(1, param{m}.num);
	for n = 1:param{m}.num
		param_value = strcat('param{', num2str(m), '}.value{', num2str(n), '}');
		cmd = strcat( ...
			param_value, ' = ', param{m}.name{n}, ';', ...
			param{m}.name{n}, ' = ', 'mpt.Parameter;', ...
			param{m}.name{n}, '.Value = ', param_value, ';' ...
			);
		if m == 1
			cmd = strcat( cmd, ...
				param{m}.name{n}, '.RTWInfo.CustomAttributes.DefinitionFile = ''ballbot_rom.c'';', ...
				param{m}.name{n}, '.RTWInfo.CustomAttributes.HeaderFile = ''ballbot_rom.h'';' ...
				);
		else
			cmd = strcat( cmd, ...
				param{m}.name{n}, '.RTWInfo.CustomStorageClass = ''Define'';', ...
				param{m}.name{n}, '.RTWInfo.CustomAttributes.HeaderFile = ''ballbot_rom.h'';' ...
				);
		end
		eval(cmd)
	end
end

% change [Function-Call source] parameter of
% the Exported Function-Calls Scheduler block to clear a worning
scheduler = find_system(gcs, 'MaskType', 'Exported Function-Calls Scheduler');
set_param(scheduler{1}, 'fctrigger', '[0, 4, 20, 100]')

clear param_value

Contact us