<commit_before>/*
 * pResource.cpp
 *
 * Author: Peter
 * Copyright (c) 2014-2015 HKUST SmartCar Team
 * Refer to LICENSE for details
 */

#include <libsc/system.h>
#include <pResource.h>
#include <pFlash.h>

using namespace libsc;
using namespace std;

// UNIQUE_VAL SHOULD NOT be 255
//
// Change it if u changed the config table
// otherwise the config table in flash memory
// won't update
#define UNIQUE_VAL	75

pResource::ConfigTable	pResource::configTable;
pResource				*pResource::m_instance = nullptr;

pResource::pResource(void)
:
	pFlash(pFlash::Config(&configTable, sizeof(pResource::ConfigTable))),
	pSmartCar()
{
	System::Init();

	if (m_instance)
		assert(false);
	else
		m_instance = this;

	if (UNIQUE_VAL != ((ConfigTable *)getConfigTablePtr())->kUniqueVal)
		reset();

	if (!getConfigTablePtr())
		assert(false);
	else
		readConfig(&configTable, ((ConfigTable *)getConfigTablePtr())->kTableSize);

	if (configTable.kIsExist)
	{
		setInitialConfigTable();
		saveConfig();
	}
	else if (configTable.kTableSize != sizeof(ConfigTable))
	{
		setInitialConfigTable();
//		addConfigToConfigTable();
		saveConfig();
	}
}

void pResource::reset(void)
{
	eraseAll();
	setInitialConfigTable();
	saveConfig();
}

void pResource::saveConfig(void)
{
	writeConfig(&configTable);
}

void pResource::grapherOnChangedListener(void)
{
	m_instance->saveConfig();
}

void pResource::setInitialConfigTable(void)
{
	configTable.kIsExist = false;
	configTable.kTableSize = sizeof(ConfigTable);
	configTable.kUniqueVal = UNIQUE_VAL;

	configTable.kAccelTruthVal = 0.02f;
	configTable.kCgHeightInM = 0.05f;

	configTable.kIdealAngle = 65.0f;
	configTable.kAngleMin = 45.0f;
	configTable.kAngleMax = 75.0f;

	configTable.kLeftMotorDeadMarginPos = 0;
	configTable.kLeftMotorDeadMarginNag = 0;
	configTable.kRightMotorDeadMarginPos = 0;
	configTable.kRightMotorDeadMarginNag = 0;

	configTable.kAngleKp = 3000.0f;
	configTable.kAngleKi = 0.0f;
	configTable.kAngleKd = 0.0f;

	configTable.kDirectionKp = 10.0f;
	configTable.kDirectionKi = 0.0f;
	configTable.kDirectionKd = 0.0f;

	configTable.kSpeedKp = 0.77f;
	configTable.kSpeedKi = 0.0f;
	configTable.kSpeedKd = 0.0f;

	configTable.kKalmanKq = 0.1f;
	configTable.kKalmanKr = 0.9f;

	configTable.kCountPerDeg = 9560 / 360.0f;
}

void pResource::addConfigToConfigTable(void)
{
	ConfigTable tempTable;
	memcpy(&tempTable, &configTable, configTable.kTableSize);
	size_t oldTableSize = tempTable.kTableSize;;
	setInitialConfigTable();
	tempTable.kTableSize = sizeof(ConfigTable);
	memcpy(&configTable, &tempTable, oldTableSize);
}<commit_msg>Save config table<commit_after>/*
 * pResource.cpp
 *
 * Author: Peter
 * Copyright (c) 2014-2015 HKUST SmartCar Team
 * Refer to LICENSE for details
 */

#include <libsc/system.h>
#include <pResource.h>
#include <pFlash.h>

using namespace libsc;
using namespace std;

// UNIQUE_VAL SHOULD NOT be 255
//
// Change it if u changed the config table
// otherwise the config table in flash memory
// won't update
#define UNIQUE_VAL	75

pResource::ConfigTable	pResource::configTable;
pResource				*pResource::m_instance = nullptr;

pResource::pResource(void)
:
	pFlash(pFlash::Config(&configTable, sizeof(pResource::ConfigTable))),
	pSmartCar()
{
	System::Init();

	if (m_instance)
		assert(false);
	else
		m_instance = this;

	if (UNIQUE_VAL != ((ConfigTable *)getConfigTablePtr())->kUniqueVal)
		reset();

	if (!getConfigTablePtr())
		assert(false);
	else
		readConfig(&configTable, ((ConfigTable *)getConfigTablePtr())->kTableSize);

	if (configTable.kIsExist)
	{
		setInitialConfigTable();
		saveConfig();
	}
	else if (configTable.kTableSize != sizeof(ConfigTable))
	{
		setInitialConfigTable();
//		addConfigToConfigTable();
		saveConfig();
	}
}

void pResource::reset(void)
{
	eraseAll();
	setInitialConfigTable();
	saveConfig();
}

void pResource::saveConfig(void)
{
	writeConfig(&configTable);
}

void pResource::grapherOnChangedListener(void)
{
	m_instance->saveConfig();
}

void pResource::setInitialConfigTable(void)
{
	configTable.kIsExist = false;
	configTable.kTableSize = sizeof(ConfigTable);
	configTable.kUniqueVal = UNIQUE_VAL;

	configTable.kAccelTruthVal = 0.02f;
	configTable.kCgHeightInM = 0.05f;

	configTable.kIdealAngle = 65.0f;
	configTable.kAngleMin = 45.0f;
	configTable.kAngleMax = 75.0f;

	configTable.kLeftMotorDeadMarginPos = 0;
	configTable.kLeftMotorDeadMarginNag = 0;
	configTable.kRightMotorDeadMarginPos = 0;
	configTable.kRightMotorDeadMarginNag = 0;

	configTable.kAngleKp = 3500.0f;
	configTable.kAngleKi = 0.0f;
	configTable.kAngleKd = 0.0f;

	configTable.kDirectionKp = 0.865f;
	configTable.kDirectionKi = 0.0f;
	configTable.kDirectionKd = 0.0015f;

	configTable.kSpeedKp = 1.1f;
	configTable.kSpeedKi = 0.0f;
	configTable.kSpeedKd = 0.001f;

	configTable.kKalmanKq = 0.1f;
	configTable.kKalmanKr = 0.9f;

	configTable.kCountPerDeg = 9560 / 360.0f;
}

void pResource::addConfigToConfigTable(void)
{
	ConfigTable tempTable;
	memcpy(&tempTable, &configTable, configTable.kTableSize);
	size_t oldTableSize = tempTable.kTableSize;;
	setInitialConfigTable();
	tempTable.kTableSize = sizeof(ConfigTable);
	memcpy(&configTable, &tempTable, oldTableSize);
}
{'fraction_non_alphanumeric': 0.12584704743465633, 'fraction_numerical': 0.02691190706679574, 'mean_word_length': 5.4818067754077795, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4507915', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1893, 'n_words': 340}