Ironcup Mar 2020

Dependencies:   mbed mbed-rtos MotionSensor EthernetInterface

Protocol/receiver.cpp

Committer:
drelliak
Date:
2016-04-11
Revision:
0:88faaa1afb83
Child:
8:a1067fcde341

File content as of revision 0:88faaa1afb83:

/*
Copyright 2016 Erik Perillo <erik.perillo@gmail.com>

This file is part of piranha-ptc.

This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this. If not, see <http://www.gnu.org/licenses/>.
*/


#include "receiver.h"


uint16_t read(Serial& serial)
{
	int i;
	uint16_t value = 0;
	char chr;
	
	for(i=0; i<2; i++)
		while(true)
			if(serial.readable())
			{
				chr = serial.getc();
				value = value | chr << i*8;	
				break;
			}

	return value;
}

float un_scale(uint16_t value, float min, float max)
{
	return ((float)value)/((1 << 16) - 1)*(max - min) + min;
}

float get_param(Serial& serial, float min, float max)
{
	uint16_t value;
	
	value = read(serial);

	return un_scale(value, min, max);
}

void get_pid_params(Serial& serial, float* kp, float* ki, float* kd, float* n)
{
	*kp = get_pid_param(serial);
	*ki = get_pid_param(serial);
	*kd = get_pid_param(serial);
	*n = get_pid_param(serial);
}