using namespace std;
CurrentControl::CurrentControl(){
- digipot= NULL;
+ digipot= NULL;
}
void CurrentControl::on_module_loaded(){
- if( !this->kernel->config->value( currentcontrol_module_enable_checksum )->by_default(false)->as_bool() ){
- // as this module is not needed free up the resource
- delete this;
- return;
- }
+ if( !this->kernel->config->value( currentcontrol_module_enable_checksum )->by_default(false)->as_bool() ){
+ // as this module is not needed free up the resource
+ delete this;
+ return;
+ }
- // allocate digipot, if already allocated delete it first
- delete digipot;
+ // allocate digipot, if already allocated delete it first
+ delete digipot;
- // see which chip to use
- int chip_checksum = get_checksum(this->kernel->config->value(digipotchip_checksum)->by_default("mcp4451")->as_string());
- if(chip_checksum == mcp4451_checksum) {
- digipot = new MCP4451();
- }else if(chip_checksum == ad5206_checksum) {
- digipot = new AD5206();
- }else { // need a default so use smoothie
- digipot = new MCP4451();
- }
+ // see which chip to use
+ int chip_checksum = get_checksum(this->kernel->config->value(digipotchip_checksum)->by_default("mcp4451")->as_string());
+ if(chip_checksum == mcp4451_checksum) {
+ digipot = new MCP4451();
+ }else if(chip_checksum == ad5206_checksum) {
+ digipot = new AD5206();
+ }else { // need a default so use smoothie
+ digipot = new MCP4451();
+ }
// Get configuration
this->alpha_current = this->kernel->config->value(alpha_current_checksum )->by_default(0.8)->as_number();
#define gamma_current_checksum CHECKSUM("gamma_current")
#define delta_current_checksum CHECKSUM("delta_current")
#define currentcontrol_module_enable_checksum CHECKSUM("currentcontrol_module_enable")
-#define digipotchip_checksum CHECKSUM("digipotchip")
+#define digipotchip_checksum CHECKSUM("digipotchip")
-#define mcp4451_checksum CHECKSUM("mcp4451")
-#define ad5206_checksum CHECKSUM("ad5206")
+#define mcp4451_checksum CHECKSUM("mcp4451")
+#define ad5206_checksum CHECKSUM("ad5206")
class CurrentControl : public Module {
public:
class DigipotBase {
public:
DigipotBase(){}
- virtual ~DigipotBase(){}
+ virtual ~DigipotBase(){}
virtual void set_current( int channel, double current )= 0;
virtual double get_current(int channel)= 0;
class AD5206 : public DigipotBase {
public:
AD5206(){
- this->spi= new mbed::SPI(P0_9,P0_8,P0_7); //should be able to set those pins in config
- cs.from_string("4.29")->as_output(); //this also should be configurable
- cs.set(1);
+ this->spi= new mbed::SPI(P0_9,P0_8,P0_7); //should be able to set those pins in config
+ cs.from_string("4.29")->as_output(); //this also should be configurable
+ cs.set(1);
}
void set_current( int channel, double current )
{
- current = min( max( current, 0.0L ), 2.0L );
+ current = min( max( current, 0.0L ), 2.0L );
- char adresses[6] = { 0x05, 0x03, 0x01, 0x00, 0x02, 0x04 };
- cs.set(0);
- spi->write((int)adresses[channel]);
- spi->write((int)current_to_wiper(current));
- cs.set(1);
+ char adresses[6] = { 0x05, 0x03, 0x01, 0x00, 0x02, 0x04 };
+ cs.set(0);
+ spi->write((int)adresses[channel]);
+ spi->write((int)current_to_wiper(current));
+ cs.set(1);
}
//taken from 4pi firmware
unsigned char current_to_wiper( double current ){
- unsigned int count = int((current*1000)*100/743); //6.8k resistor and 10k pot
+ unsigned int count = int((current*1000)*100/743); //6.8k resistor and 10k pot
- return (unsigned char)count;
- }
+ return (unsigned char)count;
+ }
double get_current(int channel)
{
return currents[channel];
}
- private:
+ private:
Pin cs;
mbed::SPI* spi;
this->i2c = new mbed::I2C(p9, p10);
for (int i = 0; i < 4; i++)
currents[i] = 0.0;
- }
-
- ~MCP4451(){
- delete this->i2c;
- }
+ }
+
+ ~MCP4451(){
+ delete this->i2c;
+ }
void set_current( int channel, double current )
{
double get_current(int channel)
{
return currents[channel];
- }
-
- private:
-
- void i2c_send( char first, char second, char third ){
- this->i2c->start();
- this->i2c->write(first);
- this->i2c->write(second);
- this->i2c->write(third);
- this->i2c->stop();
- }
-
- char current_to_wiper( double current ){
- return char(ceil(double((113.33*current))));
- }
-
- mbed::I2C* i2c;
+ }
+
+ private:
+
+ void i2c_send( char first, char second, char third ){
+ this->i2c->start();
+ this->i2c->write(first);
+ this->i2c->write(second);
+ this->i2c->write(third);
+ this->i2c->stop();
+ }
+
+ char current_to_wiper( double current ){
+ return char(ceil(double((113.33*current))));
+ }
+
+ mbed::I2C* i2c;
double currents[4];
};