Skip to content

Commit

Permalink
update Teensy pgns
Browse files Browse the repository at this point in the history
  • Loading branch information
SK21 committed Dec 8, 2024
1 parent 354a0f0 commit b5e4490
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 65 deletions.
45 changes: 19 additions & 26 deletions Modules/Teensy Rate/RCteensy/Begin.ino
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

void DoSetup()
{
uint8_t ErrorCount;
int ADS[] = { 0x48,0x49,0x4A,0x4B }; // ADS1115 addresses
uint8_t ErrorCount = 0;

//watchdog timer
WDT_timings_t config;
Expand Down Expand Up @@ -39,12 +38,10 @@ void DoSetup()
Wire.setClock(400000); //Increase I2C data rate to 400kHz

// ADS1115
for (int i = 0; i < 4; i++)
if (MDL.ADS1115Enabled)
{
ADS1115_Address = ADS[i];
Serial.print("Starting ADS1115 at address ");
Serial.print(ADS1115_Address);
ErrorCount = 0;
Serial.println(ADS1115_Address);
while (!ADSfound)
{
Wire.beginTransmission(ADS1115_Address);
Expand All @@ -59,22 +56,16 @@ void DoSetup()
Serial.println("");
if (ADSfound)
{
Serial.print("ADS1115 connected at address ");
Serial.println(ADS1115_Address);
Serial.println("ADS1115 found.");
Serial.println("");
break;
}
else
{
Serial.print("ADS1115 not found.");
Serial.println("ADS1115 not found.");
Serial.println("ADS1115 disabled.");
Serial.println("");
}
}
if (!ADSfound)
{
Serial.println("ADS1115 disabled.");
Serial.println("");
}

// ethernet
Serial.println("Starting Ethernet ...");
Expand Down Expand Up @@ -211,7 +202,7 @@ void DoSetup()
while (!MCP23017_found)
{
Serial.print(".");
Wire.beginTransmission(0x20);
Wire.beginTransmission(MCP23017address);
MCP23017_found = (Wire.endTransmission() == 0);
ErrorCount++;
delay(500);
Expand Down Expand Up @@ -254,7 +245,7 @@ void LoadData()
{
// load stored data
Serial.println("Loading stored settings.");
EEPROM.get(110, MDL);
EEPROM.get(92, MDL);

for (int i = 0; i < MaxProductCount; i++)
{
Expand All @@ -279,7 +270,7 @@ void SaveData()
Serial.println("Updating stored settings.");
EEPROM.put(0, InoID);
EEPROM.put(4, InoType);
EEPROM.put(110, MDL);
EEPROM.put(92, MDL);

for (int i = 0; i < MaxProductCount; i++)
{
Expand Down Expand Up @@ -323,26 +314,28 @@ void LoadDefaults()

// module settings
MDL.ID = 0;
MDL.SensorCount = 2;
MDL.RelayOnSignal = 1;
MDL.FlowOnDirection = 1;
MDL.SensorCount = 1;
MDL.InvertRelay = false;
MDL.InvertFlow = false;
MDL.IP0 = 192;
MDL.IP1 = 168;
MDL.IP2 = 1;
MDL.IP3 = 50;
MDL.RelayControl = 1;
MDL.ESPserialPort = 1;
MDL.WifiMode = 1;
MDL.WifiModeUseStation = false;
MDL.WorkPin = NC;
MDL.WorkPinIsMomentary = false;
MDL.Is3Wire = true;
MDL.ADS1115Enabled = false;

// network name
memset(MDL.NetName, '\0', sizeof(MDL.NetName)); // erase old name
memcpy(MDL.NetName, &DefaultNetName, 14);
memset(MDL.SSID, '\0', sizeof(MDL.SSID)); // erase old name
memcpy(MDL.SSID, &DefaultNetName, 14);

// network password
memset(MDL.NetPassword, '\0', sizeof(MDL.NetPassword)); // erase old name
memcpy(MDL.NetPassword, &DefaultNetPassword, 14);
memset(MDL.Password, '\0', sizeof(MDL.Password)); // erase old name
memcpy(MDL.Password, &DefaultNetPassword, 14);
}

bool ValidData()
Expand Down
14 changes: 7 additions & 7 deletions Modules/Teensy Rate/RCteensy/Motor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ void AdjustFlow()
if (Sensor[i].PWM >= 0)
{
//increase
digitalWrite(Sensor[i].DirPin, MDL.FlowOnDirection);
digitalWrite(Sensor[i].DirPin, MDL.InvertFlow);
analogWrite(Sensor[i].PWMPin, Sensor[i].PWM);
}
else
{
//decrease
digitalWrite(Sensor[i].DirPin, !MDL.FlowOnDirection);
digitalWrite(Sensor[i].DirPin, !MDL.InvertFlow);
analogWrite(Sensor[i].PWMPin, -Sensor[i].PWM); // offsets the negative pwm value
}
break;
Expand All @@ -28,20 +28,20 @@ void AdjustFlow()
{
if (Sensor[i].PWM >= 0)
{
digitalWrite(Sensor[i].DirPin, MDL.FlowOnDirection);
digitalWrite(Sensor[i].DirPin, MDL.InvertFlow);
analogWrite(Sensor[i].PWMPin, Sensor[i].PWM);
}
else
{
//decrease
digitalWrite(Sensor[i].DirPin, !MDL.FlowOnDirection);
digitalWrite(Sensor[i].DirPin, !MDL.InvertFlow);
analogWrite(Sensor[i].PWMPin, -Sensor[i].PWM); // offsets the negative pwm value
}
}
else
{
// stop flow
digitalWrite(Sensor[i].DirPin, !MDL.FlowOnDirection);
digitalWrite(Sensor[i].DirPin, !MDL.InvertFlow);
analogWrite(Sensor[i].PWMPin, 255);
}
break;
Expand All @@ -54,13 +54,13 @@ void AdjustFlow()
if (Sensor[i].PWM >= 0)
{
//increase
digitalWrite(Sensor[i].DirPin, MDL.FlowOnDirection);
digitalWrite(Sensor[i].DirPin, MDL.InvertFlow);
analogWrite(Sensor[i].PWMPin, Sensor[i].PWM);
}
else
{
//decrease
digitalWrite(Sensor[i].DirPin, !MDL.FlowOnDirection);
digitalWrite(Sensor[i].DirPin, !MDL.InvertFlow);
analogWrite(Sensor[i].PWMPin, -Sensor[i].PWM); // offsets the negative pwm value
}
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/Teensy Rate/RCteensy/PID.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ double LastPWM[MaxProductCount];
bool PauseAdjust[MaxProductCount];
uint32_t ComboTime[MaxProductCount];
const double MinStart = 0.03; // minimum start ratio. Used to quickly increase rate from 0.
int TimedAdjustTime = 80; // milliseconds
int TimedPauseTime = 400; // milliseconds
uint TimedAdjustTime = 80; // milliseconds
uint TimedPauseTime = 400; // milliseconds

void SetPWM()
{
Expand Down
34 changes: 21 additions & 13 deletions Modules/Teensy Rate/RCteensy/RCteensy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,46 @@ extern "C" {
}

// rate control with Teensy 4.1
# define InoDescription "RCteensy : 30-Nov-2024"
const uint16_t InoID = 30114; // change to send defaults to eeprom, ddmmy, no leading 0
# define InoDescription "RCteensy : 06-Dec-2024"
const uint16_t InoID = 6124; // change to send defaults to eeprom, ddmmy, no leading 0
const uint8_t InoType = 1; // 0 - Teensy AutoSteer, 1 - Teensy Rate, 2 - Nano Rate, 3 - Nano SwitchBox, 4 - ESP Rate

#define MaxReadBuffer 100 // bytes
#define MaxProductCount 2
#define NC 0xFF // Pins not connected
#define ModStringLengths 15

const int16_t ADS1115_Address = 0x48;
const uint8_t MCP23017address = 0x20;
const uint8_t PCF8574address = 0x20;

uint8_t DefaultRelayPins[] = {8,9,10,11,12,25,26,27,NC,NC,NC,NC,NC,NC,NC,NC}; // pin numbers when GPIOs are used for relay control (1), default RC11
char DefaultNetName[ModStringLengths] = "Tractor"; // name of network ESP32 connects to
char DefaultNetPassword[ModStringLengths] = "111222333";

struct ModuleConfig
{
uint8_t ID = 0;
uint8_t SensorCount = 2; // up to 2 sensors, if 0 rate control will be disabled
uint8_t RelayOnSignal = 1; // value that turns on relays
uint8_t FlowOnDirection = 1; // sets on value for flow valve or sets motor direction
uint8_t SensorCount = 1; // up to 2 sensors, if 0 rate control will be disabled
bool InvertRelay = false; // value that turns on relays
bool InvertFlow = false; // sets on value for flow valve or sets motor direction
uint8_t IP0 = 192;
uint8_t IP1 = 168;
uint8_t IP2 = 1;
uint8_t IP3 = 50;
uint8_t RelayControl = 1; // 0 - no relays, 1 - GPIOs, 2 - PCA9555 8 relays, 3 - PCA9555 16 relays, 4 - MCP23017, 5 - PCA9685 single , 6 - PCA9685 paired
uint8_t ESPserialPort = 1; // serial port to connect to wifi module
uint8_t RelayPins[16];
uint8_t WifiMode = 1; // 0 AP mode, 1 Station + AP
char NetName[ModStringLengths];
char NetPassword[ModStringLengths];
uint8_t WorkPin = NC;
uint8_t RelayPins[16] = { 8,9,10,11,12,25,26,27,NC,NC,NC,NC,NC,NC,NC,NC }; // pin numbers when GPIOs are used for relay control (1), default RC11
uint8_t RelayControl = 4; // 0 - no relays, 1 - GPIOs, 2 - PCA9555 8 relays, 3 - PCA9555 16 relays, 4 - MCP23017, 5 - PCA9685, 6 - PCF8574
char APname[ModStringLengths] = "RateModule";
char APpassword[ModStringLengths] = "111222333";
bool WifiModeUseStation = false; // false - AP mode, true - AP + Station
char SSID[ModStringLengths] = "Tractor"; // name of network ESP32 connects to
char Password[ModStringLengths] = "111222333";
uint8_t WorkPin = 2;
bool WorkPinIsMomentary = false;
bool Is3Wire = true; // False - DRV8870 provides powered on/off with Output1/Output2, True - DRV8870 provides on/off with Output2 only, Output1 is off
uint8_t PressurePin = 15; // NC - no pressure pin
bool ADS1115Enabled = false;
uint8_t ESPserialPort = 1; // serial port to connect to wifi module
};

ModuleConfig MDL;
Expand Down Expand Up @@ -120,7 +129,6 @@ struct AnalogConfig
};
AnalogConfig AINs;

int ADS1115_Address;
bool ADSfound = false;

// WifiSwitches connection to ESP8266
Expand Down
2 changes: 1 addition & 1 deletion Modules/Teensy Rate/RCteensy/RCteensy.vcxproj

Large diffs are not rendered by default.

26 changes: 15 additions & 11 deletions Modules/Teensy Rate/RCteensy/Receive.ino
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,12 @@ void ReadPGNs(byte Data[], uint16_t len)
//2 Module ID 0-15
//3 sensor count
//4 commands
// bit 0 - Relay on high
// bit 1 - Flow on high
// bit 2 - client mode
// bit 0 - Invert relay control
// bit 1 - Invert flow control
// bit 2 - wifi station/client mode enabled
// bit 3 - work pin is momentary
// bit 4 - Is3Wire valve
// bit 5 - ADS1115 enabled
//5 relay control type 0 - no relays, 1 - GPIOs, 2 - PCA9555 8 relays, 3 - PCA9555 16 relays, 4 - MCP23017
// , 5 - PCA9685, 6 - PCF8574
//6 wifi module serial port
Expand All @@ -338,10 +339,12 @@ void ReadPGNs(byte Data[], uint16_t len)
MDL.SensorCount = Data[3];

byte tmp = Data[4];
if ((tmp & 1) == 1) MDL.RelayOnSignal = 1; else MDL.RelayOnSignal = 0;
if ((tmp & 2) == 2) MDL.FlowOnDirection = 1; else MDL.FlowOnDirection = 0;
if ((tmp & 4) == 4) MDL.WifiMode = 1; else MDL.WifiMode = 0;
if ((tmp & 8) == 8) MDL.WorkPinIsMomentary = 1; else MDL.WorkPinIsMomentary = 0;
MDL.InvertRelay = ((tmp & 1) == 1);
MDL.InvertFlow = ((tmp & 2) == 2);
MDL.WifiModeUseStation = ((tmp & 4) == 4);
MDL.WorkPinIsMomentary = ((tmp & 8) == 8);
MDL.Is3Wire = ((tmp & 16) == 16);
MDL.ADS1115Enabled = ((tmp & 32) == 32);

MDL.RelayControl = Data[5];
Sensor[0].FlowPin = Data[7];
Expand All @@ -357,6 +360,7 @@ void ReadPGNs(byte Data[], uint16_t len)
}

MDL.WorkPin = Data[29];
MDL.PressurePin = Data[30];

//SaveData(); saved in pgn 3702
}
Expand All @@ -378,12 +382,12 @@ void ReadPGNs(byte Data[], uint16_t len)
if (GoodCRC(Data, PGNlength))
{
// network name
memset(MDL.NetName, '\0', sizeof(MDL.NetName)); // erase old name
memcpy(MDL.NetName, &Data[2], 14);
memset(MDL.SSID, '\0', sizeof(MDL.SSID)); // erase old name
memcpy(MDL.SSID, &Data[2], 14);

// network password
memset(MDL.NetPassword, '\0', sizeof(MDL.NetPassword)); // erase old name
memcpy(MDL.NetPassword, &Data[17], 14);
memset(MDL.Password, '\0', sizeof(MDL.Password)); // erase old name
memcpy(MDL.Password, &Data[17], 14);

SaveData();
SendNetworkConfig();
Expand Down
6 changes: 3 additions & 3 deletions Modules/Teensy Rate/RCteensy/Relays.ino
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void CheckRelays()
{
if (MDL.RelayPins[i + j * 8] < NC) // check if relay is enabled
{
if (bitRead(Rlys, i)) digitalWrite(MDL.RelayPins[i + j * 8], MDL.RelayOnSignal); else digitalWrite(MDL.RelayPins[i + j * 8], !MDL.RelayOnSignal);
if (bitRead(Rlys, i)) digitalWrite(MDL.RelayPins[i + j * 8], MDL.InvertRelay); else digitalWrite(MDL.RelayPins[i + j * 8], !MDL.InvertRelay);
}
}
}
Expand Down Expand Up @@ -143,11 +143,11 @@ void CheckRelays()
{
if (bitRead(Rlys, i))
{
MCP.digitalWrite(IOpin, MDL.RelayOnSignal);
MCP.digitalWrite(IOpin, MDL.InvertRelay);
}
else
{
MCP.digitalWrite(IOpin, !MDL.RelayOnSignal);
MCP.digitalWrite(IOpin, !MDL.InvertRelay);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/Teensy Rate/RCteensy/Send.ino
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ void SendNetworkConfig()

for (int i = 0; i < 15; i++)
{
Data[i + 2] = MDL.NetName[i];
Data[i + 17] = MDL.NetPassword[i];
Data[i + 2] = MDL.SSID[i];
Data[i + 17] = MDL.Password[i];
}

Data[32] = CRC(Data, PGNlength - 1, 0);
Expand Down

0 comments on commit b5e4490

Please sign in to comment.