public void SetFailsafe(bool setting, ILogger log = null)
        {
            if (!_CheckInit())
            {
                return;
            }

            bool currentState = this.GetFailsafe(log);

            if (currentState == setting)
            {
                if (log != null)
                {
                    log.WriteLog("Called setting for failsafe: " + setting.ToString() + " but it already was...");
                }
                return;
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_FAILSAFE, Convert.ToByte(!currentState) });
                if (log != null)
                {
                    log.WriteLog("Set failsafe to " + (!currentState).ToString());
                }
            }
        }
        public bool GetFailsafe(ILogger log = null)
        {
            if (!_CheckInit())
            {
                throw new NullReferenceException("ThunderBorg_class not initiated.");
            }

            bool tempReturn = false;

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_FAILSAFE });
                byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN);
                if (log != null)
                {
                    log.WriteLog("Got response on failsafe: " + BytesToString(response));
                }

                if (response[0] == COMMAND_GET_FAILSAFE)
                {
                    tempReturn = Convert.ToBoolean(response[1]);
                }
            }

            return(tempReturn);
        }
        public byte[] GetDriveFaultB(ILogger log = null)
        {
            if (!_CheckInit(log, true))
            {
                return(null);
            }

            byte[] tempReturn;

            if (log != null)
            {
                log.WriteLog("Getting drive fault B status...");
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_DRIVE_B_FAULT });
                tempReturn = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN);

                if (log != null)
                {
                    log.WriteLog("Raw Response: " + BytesToString(tempReturn));
                }
            }

            return(tempReturn);
        }
        public byte GetBoardID(ILogger log = null)
        {
            byte tempReturn = 0x00;

            if (!_CheckInit())
            {
                throw new NullReferenceException("ThunderBorg_class not initiated.");
            }

            if (log != null)
            {
                log.WriteLog("Checking board ID...", ILogger.Priority.Information);
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_ID });
                byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN);
                if (response[0] == COMMAND_GET_ID)
                {
                    tempReturn = response[1];
                }
            }

            return(tempReturn);
        }
        /// <summary>
        /// Sets the battery monitoring limits used for setting the LED color.  The colors shown range from full red at minimum (or below), yellow at half, and green at maximum or higher.  These values are stored in the EEPROM and reloaded when the board is powered.
        /// </summary>
        /// <param name="minimum">Minimum voltage (0 V minimum)</param>
        /// <param name="maximum">Maximum voltage (36.3 V maximum)</param>
        /// <param name="log">Optional logging output routine</param>
        public void SetBatteryMonitoringLimits(decimal minimum, decimal maximum, ILogger log = null)
        {
            // my original values were 6.98 / 35.02; I don't know what the defaults are
            if (!_CheckInit())
            {
                throw new NullReferenceException("ThunderBorg_class not initiated.");
            }

            minimum /= Convert.ToDecimal(VOLTAGE_PIN_MAX);
            maximum /= Convert.ToDecimal(VOLTAGE_PIN_MAX);

            byte levelMin = Math.Max(Convert.ToByte(0x00), Math.Min(Convert.ToByte(0xFF), Convert.ToByte(minimum * 0xFF)));
            byte levelMax = Math.Max(Convert.ToByte(0x00), Math.Min(Convert.ToByte(0xFF), Convert.ToByte(maximum * 0xFF)));

            if (log != null)
            {
                log.WriteLog("Trying to set battery monitoring limits to: 0x" + levelMin.ToString("X2") + " V. min and 0x" + levelMax.ToString("X2") + " V. max...", ILogger.Priority.Information);
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_BATT_LIMITS, levelMin, levelMax });
                System.Threading.Thread.Sleep(200);     // note: this was recommended in the Python version for EEPROM writing
            }
        }
        public int GetMotorB(ILogger log = null)
        {
            int tempReturn = 0;

            if (!_CheckInit(log, true))
            {
                return(0);
            }

            if (log != null)
            {
                log.WriteLog("Getting power level for B motors...");
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_B });
                byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN);
                if (response == null)
                {
                    if (log != null)
                    {
                        log.WriteLog("*** ERROR: no response from ThunderBorg_class...");
                    }

                    throw new NullReferenceException("No parseable response from B motors on GetMotorB request.");
                }
                else if (response[0] != COMMAND_GET_B)
                {
                    if (log != null)
                    {
                        log.WriteLog("Didn't get an expected response from the B motors on GetMotorB request.");
                    }

                    throw new IndexOutOfRangeException("Unexpected response from B motors on GetMotorB request.");
                }
                else
                {
                    byte power_direction = response[1];
                    byte power_level     = response[2];

                    if (log != null)
                    {
                        log.WriteLog("Raw response: " + BytesToString(response));
                    }

                    if (power_direction == COMMAND_VALUE_FWD)
                    {
                        tempReturn = power_level;
                    }
                    else if (power_direction == COMMAND_VALUE_REV)
                    {
                        tempReturn = -power_level;
                    }
                }
            }

            return(tempReturn);
        }
        public void SetLED2(byte red, byte green, byte blue, ILogger log = null)
        {
            if (!_CheckInit())
            {
                return;
            }

            if (log != null)
            {
                log.WriteLog("Setting LED 2...", ILogger.Priority.Information);
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_LED2, red, green, blue });
            }
        }
        public void SetLEDBatteryMonitor(bool setting, ILogger log = null)
        {
            if (!_CheckInit())
            {
                return;
            }

            if (log != null)
            {
                log.WriteLog("Setting LED battery monitor to: " + setting.ToString(), ILogger.Priority.Information);
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_LED_BATT_MON, Convert.ToByte(setting) });
            }
        }
        public void AllStop(ILogger log = null)
        {
            if (!_CheckInit(log, true))
            {
                return;
            }

            if (log != null)
            {
                log.WriteLog("Calling all stop.");
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_ALL_OFF });
            }
        }
示例#10
0
        public void SetAllMotors(int power, ILogger log = null)
        {
            if (!_CheckInit())
            {
                return;
            }

            if ((power > 255) || (power < -255))
            {
                if (log != null)
                {
                    log.WriteLog("Power level out of range -255 <= power <= 255; rejecting command...");
                }
                throw new IndexOutOfRangeException("Invalid power setting to motors; range outside of -255 <= power <= 255.");
            }

            if (log != null)
            {
                log.WriteLog("Setting all motors to: " + power.ToString("X2"));
            }

            byte parsedPower = 0;

            if (power > 0)
            {
                parsedPower = Convert.ToByte(power);
            }
            else
            {
                parsedPower = Convert.ToByte(-power);
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                if (power > 0)
                {
                    bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_ALL_FWD, parsedPower });
                }
                else
                {
                    bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_ALL_REV, parsedPower });
                }
            }
        }
示例#11
0
        public void SetLEDs(byte red, byte green, byte blue, ILogger log = null)
        {
            if (!_CheckInit())
            {
                return;
            }

            if (log != null)
            {
                log.WriteLog("Setting LEDs to " + red.ToString() + ", " + green.ToString() + ", " + blue.ToString() + "...", ILogger.Priority.Information);
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_SET_LEDS, red, green, blue });

                byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN);
            }
        }
示例#12
0
        public static int ScanForThunderBorg(int busNumber = 1, ILogger log = null)
        {
            int tempReturn = -1;

            if (log != null)
            {
                log.WriteLog("Starting scan for ThunderBorg_class board...");
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + busNumber.ToString()))
            {
                for (byte port = 0x03; port < 0x78; port++)
                {
                    try
                    {
                        bus.WriteByte(port, COMMAND_GET_ID);
                        byte[] response = bus.ReadBytes(port, I2C_MAX_LEN);
                        if (response[0] == 0x99)
                        {
                            if (response[1] == I2C_ID_THUNDERBORG)
                            {
                                tempReturn = port;
                                if (log != null)
                                {
                                    log.WriteLog("FOUND ThunderBorg_class board on port: " + port.ToString("X2"));
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // do nothing
                    }
                }
            }

            if (log != null)
            {
                log.WriteLog("Finished port scan...");
            }

            return(tempReturn);
        }
示例#13
0
        public decimal[] GetBatteryMonitoringLimits(ILogger log = null)
        {
            decimal tempMin = 0.00M;
            decimal tempMax = 0.00M;

            if (!_CheckInit())
            {
                throw new NullReferenceException("ThunderBorg_class not initiated.");
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_BATT_LIMITS });
                byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN);
                if (response[0] == COMMAND_GET_BATT_LIMITS)
                {
                    tempMin = response[1];
                    tempMax = response[2];

                    tempMin /= Convert.ToDecimal(0xFF);
                    tempMax /= Convert.ToDecimal(0xFF);

                    tempMin *= VOLTAGE_PIN_MAX;
                    tempMax *= VOLTAGE_PIN_MAX;

                    tempMin = Math.Round(tempMin, 2);
                    tempMax = Math.Round(tempMax, 2);

                    decimal[] tempReturn = { tempMin, tempMax };

                    if (log != null)
                    {
                        log.WriteLog("Voltages found - min: " + tempMin.ToString() + " max: " + tempMax.ToString(), ILogger.Priority.Information);
                    }

                    return(tempReturn);
                }
            }

            return(null);
        }
示例#14
0
        public decimal GetBatteryVoltage(ILogger log = null)
        {
            int     tempIntReturn = 0;
            decimal tempReturn    = 0.00M;

            if (!_CheckInit())
            {
                throw new NullReferenceException("ThunderBorg_class not initiated.");
            }

            if (log != null)
            {
                log.WriteLog("Getting battery voltage...", ILogger.Priority.Information);
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_BATT_VOLT });
                byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN);

                if (log != null)
                {
                    log.WriteLog("GET_BATT_VOLT response: " + this.BytesToString(response), ILogger.Priority.Information);
                }

                if (response[0] == COMMAND_GET_BATT_VOLT)
                {
                    tempIntReturn = ((int)response[1] << 8) + (int)response[2];
                }
            }

            tempReturn = Convert.ToDecimal(tempIntReturn) / Convert.ToDecimal(COMMAND_ANALOG_MAX);

            tempReturn *= VOLTAGE_PIN_MAX;
            tempReturn += VOLTAGE_PIN_CORRECTION;
            tempReturn  = Math.Round(tempReturn, 2);

            return(tempReturn);
        }
示例#15
0
        public byte[] GetLED2(ILogger log = null)
        {
            byte[] tempReturn;

            if (!_CheckInit())
            {
                return(null);
            }

            if (log != null)
            {
                log.WriteLog("Calling results for get LED 2...");
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_LED2 });

                tempReturn = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN);
                if (tempReturn[0] == COMMAND_GET_LED2)
                {
                    if (log != null)
                    {
                        log.WriteLog("Get LED2 response: " + this.BytesToString(tempReturn));
                    }

                    return(tempReturn);
                }
                else
                {
                    if (log != null)
                    {
                        log.WriteLog("Got a nonsense reponse from COMMAND_GET_LED2..." + this.BytesToString(tempReturn));
                    }

                    throw new InvalidProgramException("Nonsense response during COMMAND_GET_LED2.");
                }
            }
        }
示例#16
0
        public bool GetLEDBatteryMonitor(ILogger log = null)
        {
            bool tempReturn = false;

            if (!_CheckInit())
            {
                throw new NullReferenceException("ThunderBorg_class not initiated.");
            }

            using (var bus = I2CBus.Open("/dev/i2c-" + this._bus.ToString()))
            {
                bus.WriteBytes(_TBorgAddress, new byte[] { COMMAND_GET_LED_BATT_MON });
                byte[] response = bus.ReadBytes(_TBorgAddress, I2C_MAX_LEN);
                if (log != null)
                {
                    log.WriteLog("Response from COMMAND_GET_LED_BATT_MON: " + this.BytesToString(response));
                }
                if (response[0] == COMMAND_GET_LED_BATT_MON)
                {
                    if (response[1] == COMMAND_VALUE_ON)
                    {
                        tempReturn = true;
                        return(tempReturn);
                    }
                    else if (response[1] == COMMAND_VALUE_OFF)
                    {
                        tempReturn = false;
                        return(tempReturn);
                    }
                }
                else
                {
                    throw new InvalidProgramException("Nonsense response during COMMAND_GET_LED_BATT_MON.");
                }
            }

            return(tempReturn);
        }
示例#17
0
        public static int SetNewAddress(byte newAddress, byte?oldAddress = null, int?busNumber = 0, ILogger logger = null)
        {
            int  _oldAddress;
            bool _foundBoard = false;

            if ((newAddress <= 0x03) || (newAddress > 0x77))
            {
                logger.WriteLog("Error: I2C addresses below 3 (0x03) and above 119 (0x77) are reserved.  Please use a different address.");
                throw new ArgumentOutOfRangeException("newAddress", "New port number must be between 0x03 and 0x77.");
            }

            if (oldAddress == null)
            {
                _oldAddress = ThunderBorg_class.ScanForThunderBorg(Convert.ToInt32(busNumber), logger);
                if (_oldAddress < 0)
                {
                    throw new Exception("ThunderBorg board not found.");
                }
            }
            else
            {
                _oldAddress = Convert.ToInt32(oldAddress);
            }

            if (logger != null)
            {
                logger.WriteLog("Attempting change of ThunderBorg address from " + _oldAddress.ToString("X2") + " to " + newAddress.ToString("X2"), ILogger.Priority.Information);
            }

            // This code was intended to do another check, but on reflection it doesn't seem to do anything.
            //
            //ThunderBorg_class changeTB = new ThunderBorg_class();
            //try
            //{
            //    byte boardResponse = changeTB.GetBoardID(logger);
            //    if (boardResponse == _oldAddress)                   // HACK: are you sure?
            //    {
            //        if (logger != null)
            //        {
            //            logger.WriteLog("ThunderBorg found at " + _oldAddress.ToString("X2"), ILogger.Priority.Information);
            //        }
            //        _foundBoard = true;
            //    }
            //    else
            //    {
            //        if (logger != null)
            //        {
            //            logger.WriteLog("Found _something_ at " + _oldAddress.ToString("X2") + " but it doesn't appear to be a ThunderBorg.", ILogger.Priority.Critical);
            //        }
            //        _foundBoard = false;
            //        throw new ArgumentOutOfRangeException("oldAddress", "Found different kind of board at address " + _oldAddress.ToString("X2"));
            //    }
            //}
            //catch(Exception ex)
            //{
            //    // do something
            //}

            //if (!_foundBoard)
            //{
            //    if (logger != null)
            //    {
            //        logger.WriteLog("No board found.", ILogger.Priority.High);
            //    }
            //}

            using (var bus = I2CBus.Open("/dev/i2c-" + busNumber.ToString()))
            {
                bus.WriteBytes(_oldAddress, new byte[] { COMMAND_SET_I2C_ADD, newAddress });

                System.Threading.Thread.Sleep(200);         // let the I2C bus catch up

                int tempCheck = ThunderBorg_class.ScanForThunderBorg(Convert.ToInt32(busNumber), logger);

                if (tempCheck == newAddress)
                {
                    logger.WriteLog("CHANGED BOARD ADDRESS FROM " + _oldAddress.ToString("X2") + " TO " + newAddress.ToString("X2"), ILogger.Priority.Critical);
                    logger.WriteLog("This change will be persistent even after a reboot; keep track of it.", ILogger.Priority.Information);
                }
                else
                {
                    logger.WriteLog("**FAILED** to change ThunderBorg address.  Current address: " + tempCheck.ToString("X2"));
                }
                return(tempCheck);
            }
        }