grbl-LPC-CoreXY/grbl-lpc/current_control.cpp

70 lines
2.4 KiB
C++
Raw Normal View History

2017-01-08 00:31:12 +01:00
// Current control
//
// Copyright 2017 Todd Fleming
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "current_control.h"
#include "grbl.h"
#include "Driver_I2C.h"
#ifdef CURRENT_I2C
extern ARM_DRIVER_I2C CURRENT_I2C;
static const uint8_t wiperRegs[] = CURRENT_WIPERS;
#endif
void current_init()
{
#ifdef CURRENT_I2C
CURRENT_I2C.Initialize(nullptr);
CURRENT_I2C.PowerControl(ARM_POWER_FULL);
CURRENT_I2C.Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_STANDARD);
CURRENT_I2C.Control(ARM_I2C_BUS_CLEAR, 0);
2017-01-08 02:12:55 +01:00
#ifdef CURRENT_MCP44XX_ADDR
2017-01-08 00:31:12 +01:00
uint8_t init1[] = {0x40, 0xff};
uint8_t init2[] = {0xA0, 0xff};
CURRENT_I2C.MasterTransmit(CURRENT_MCP44XX_ADDR, init1, 2, false);
2017-01-08 02:12:55 +01:00
while (CURRENT_I2C.GetStatus().busy)
;
2017-01-08 00:31:12 +01:00
CURRENT_I2C.MasterTransmit(CURRENT_MCP44XX_ADDR, init2, 2, false);
2017-01-08 02:12:55 +01:00
while (CURRENT_I2C.GetStatus().busy)
;
#endif
2017-01-08 00:31:12 +01:00
set_current(0, DEFAULT_X_CURRENT);
set_current(1, DEFAULT_Y_CURRENT);
set_current(2, DEFAULT_Z_CURRENT);
set_current(3, DEFAULT_A_CURRENT);
#endif
}
void set_current(uint8_t motor, float amps)
{
#if defined(CURRENT_I2C) && defined(CURRENT_MCP44XX_ADDR)
uint8_t command[] = {
uint8_t(wiperRegs[motor] << 4),
uint8_t(std::round(amps * CURRENT_FACTOR)),
};
CURRENT_I2C.MasterTransmit(CURRENT_MCP44XX_ADDR, command, sizeof(command), false);
while (CURRENT_I2C.GetStatus().busy)
;
#endif
}