Compare commits
8 Commits
7e347ca442
...
main
Author | SHA1 | Date | |
---|---|---|---|
17eb4f3912 | |||
82027caed8 | |||
|
0033ad82df | ||
|
06f9f00e9b | ||
|
f5e8e277eb | ||
|
ee895125cb | ||
|
9532be0b4e | ||
|
a433406e1f |
@@ -1,17 +1,41 @@
|
||||
[package]
|
||||
name = "rust"
|
||||
name = "plant-ctrl2"
|
||||
version = "0.1.0"
|
||||
authors = ["Empire <empirephoenix@yahoo.de>"]
|
||||
authors = ["Empire Phoenix"]
|
||||
edition = "2021"
|
||||
resolver = "2"
|
||||
rust-version = "1.71"
|
||||
|
||||
[profile.release]
|
||||
[profile.dev]
|
||||
# Explicitly disable LTO which the Xtensa codegen backend has issues
|
||||
lto = false
|
||||
strip = false
|
||||
debug = true
|
||||
overflow-checks = true
|
||||
panic = "abort"
|
||||
incremental = true
|
||||
opt-level = "s"
|
||||
|
||||
[profile.dev]
|
||||
debug = true # Symbols are nice and they don't increase the size on Flash
|
||||
opt-level = "z"
|
||||
[profile.dev.build-override]
|
||||
opt-level = 1
|
||||
incremental = true
|
||||
|
||||
[package.metadata.cargo_runner]
|
||||
# The string `$TARGET_FILE` will be replaced with the path from cargo.
|
||||
command = [
|
||||
"cargo",
|
||||
"espflash",
|
||||
"save-image",
|
||||
"--partition-table",
|
||||
"partitions.csv",
|
||||
"--chip",
|
||||
"esp32c6",
|
||||
"image.bin"
|
||||
]
|
||||
|
||||
|
||||
[package.metadata.espflash]
|
||||
partition_table = "partitions.csv"
|
||||
|
||||
[features]
|
||||
default = ["std", "embassy", "esp-idf-svc/native"]
|
||||
@@ -25,11 +49,36 @@ embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-
|
||||
|
||||
[dependencies]
|
||||
log = { version = "0.4", default-features = false }
|
||||
esp-idf-svc = { version = "0.48", default-features = false }
|
||||
serde = { version = "1.0.192", features = ["derive"] }
|
||||
average = { version = "0.14.1" , features = ["std"] }
|
||||
#esp32 = "0.28.0"
|
||||
bit_field = "0.10.2"
|
||||
ds18b20 = "0.1.1"
|
||||
embedded-svc = { version = "0.27.0", features = ["experimental"] }
|
||||
esp-idf-hal = "0.43.0"
|
||||
esp-idf-sys = { version = "0.34.0", features = ["binstart", "native"] }
|
||||
esp-idf-svc = { version = "0.48.0", default-features = false }
|
||||
esp_idf_build = "0.1.3"
|
||||
chrono = { version = "0.4.23", default-features = false , features = ["iana-time-zone" , "alloc"] }
|
||||
chrono-tz = {version="0.8.0", default-features = false , features = [ "filter-by-regex" ]}
|
||||
embedded-hal = "1.0.0"
|
||||
one-wire-bus = "0.1.1"
|
||||
anyhow = { version = "1.0.75", features = ["std", "backtrace"] }
|
||||
schemars = "0.8.16"
|
||||
heapless = { version = "0.8", features = ["serde"] }
|
||||
serde_json = "1.0.108"
|
||||
strum = { version = "0.26.1", features = ["derive"] }
|
||||
once_cell = "1.19.0"
|
||||
measurements = "0.11.0"
|
||||
bq34z100 = "0.2.1"
|
||||
|
||||
[patch.crates-io]
|
||||
#esp-idf-hal = { git = "https://github.com/esp-rs/esp-idf-hal.git" }
|
||||
esp-idf-hal = { git = "https://github.com/empirephoenix/esp-idf-hal.git" }
|
||||
#esp-idf-sys = { git = "https://github.com/empirephoenix/esp-idf-sys.git" }
|
||||
#esp-idf-sys = { git = "https://github.com/esp-rs/esp-idf-sys.git" }
|
||||
#esp-idf-svc = { git = "https://github.com/esp-rs/esp-idf-svc.git" }
|
||||
|
||||
[build-dependencies]
|
||||
embuild = "0.31.3"
|
||||
vergen = { version = "8.2.6", features = ["build", "git", "gitcl"] }
|
||||
|
@@ -1,10 +1,10 @@
|
||||
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
|
||||
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000
|
||||
CONFIG_ESP_MAIN_TASK_STACK_SIZE=25000
|
||||
|
||||
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
|
||||
# This allows to use 1 ms granuality for thread sleeps (10 ms by default).
|
||||
#CONFIG_FREERTOS_HZ=1000
|
||||
CONFIG_FREERTOS_HZ=1000
|
||||
|
||||
# Workaround for https://github.com/espressif/esp-idf/issues/7631
|
||||
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n
|
||||
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n
|
||||
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
|
||||
CONFIG_I2C_ENABLE_DEBUG_LOG=y
|
||||
DEBUG_LEVEL=5
|
203
rust/src/main.rs
203
rust/src/main.rs
@@ -1,19 +1,31 @@
|
||||
use esp_idf_hal::gpio::{AnyInputPin, Gpio10, Gpio6, Gpio5, InputOutput, Level, PinDriver, Pull};
|
||||
use esp_idf_svc::hal::{peripheral::Peripheral, peripherals::Peripherals};
|
||||
use esp_idf_sys::{esp, gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError};
|
||||
use esp_idf_hal::delay::Delay;
|
||||
use std::time::Duration;
|
||||
use esp_idf_svc::eventloop::EspSystemEventLoop;
|
||||
use esp_idf_svc::ipv4::IpInfo;
|
||||
use esp_idf_svc::mqtt::client::QoS::AtLeastOnce;
|
||||
use esp_idf_svc::mqtt::client::QoS::ExactlyOnce;
|
||||
use esp_idf_svc::mqtt::client::{EspMqttClient, LwtConfiguration, MqttClientConfiguration};
|
||||
use esp_idf_svc::nvs::EspDefaultNvsPartition;
|
||||
use esp_idf_svc::wifi::config::{ScanConfig, ScanType};
|
||||
use esp_idf_svc::wifi::EspWifi;
|
||||
use esp_idf_hal::adc::{attenuation, AdcChannelDriver, AdcDriver};
|
||||
use std::sync::Mutex;
|
||||
|
||||
fn main() {
|
||||
use chrono::{DateTime, Datelike, Timelike};
|
||||
use chrono_tz::Europe::Berlin;
|
||||
|
||||
use esp_idf_hal::delay::Delay;
|
||||
use esp_idf_sys::
|
||||
esp_restart
|
||||
;
|
||||
use log::error;
|
||||
use once_cell::sync::Lazy;
|
||||
use plant_hal::{CreatePlantHal, PlantCtrlBoard, PlantCtrlBoardInteraction, PlantHal};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod plant_hal;
|
||||
|
||||
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq)]
|
||||
enum OnlineMode {
|
||||
Offline,
|
||||
Wifi,
|
||||
SnTp,
|
||||
Online,
|
||||
}
|
||||
|
||||
pub static BOARD_ACCESS: Lazy<Mutex<PlantCtrlBoard>> = Lazy::new(|| PlantHal::create().unwrap());
|
||||
|
||||
|
||||
fn safe_main() -> anyhow::Result<()> {
|
||||
// It is necessary to call this function once. Otherwise some patches to the runtime
|
||||
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
|
||||
esp_idf_svc::sys::link_patches();
|
||||
@@ -21,54 +33,127 @@ fn main() {
|
||||
// Bind the log crate to the ESP Logging facilities
|
||||
esp_idf_svc::log::EspLogger::initialize_default();
|
||||
|
||||
log::info!("Hello, world!");
|
||||
|
||||
let mut peripherals = Peripherals::take().unwrap();
|
||||
let mut in1 = PinDriver::input_output(peripherals.pins.gpio7).unwrap();
|
||||
let mut in2 = PinDriver::input_output(peripherals.pins.gpio6).unwrap();
|
||||
|
||||
let sys_loop = EspSystemEventLoop::take().unwrap();
|
||||
let nvs = EspDefaultNvsPartition::take().unwrap();
|
||||
let mut wifi_driver = EspWifi::new(peripherals.modem, sys_loop, Some(nvs)).unwrap();
|
||||
wifi_driver.start().unwrap();
|
||||
wifi_driver.start_scan(
|
||||
&ScanConfig {
|
||||
scan_type: ScanType::Passive(Duration::from_secs(1)),
|
||||
show_hidden: false,
|
||||
..Default::default()
|
||||
},
|
||||
true,
|
||||
).unwrap();
|
||||
let sr = wifi_driver.get_scan_result().unwrap();
|
||||
for r in sr.iter() {
|
||||
println!("Found wifi {}", r.ssid);
|
||||
if esp_idf_sys::CONFIG_MAIN_TASK_STACK_SIZE < 25000 {
|
||||
error!(
|
||||
"stack too small: {} bail!",
|
||||
esp_idf_sys::CONFIG_MAIN_TASK_STACK_SIZE
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let adc_config = esp_idf_hal::adc::config::Config {
|
||||
resolution: esp_idf_hal::adc::config::Resolution::Resolution12Bit,
|
||||
calibration: true,
|
||||
log::info!("Startup Rust");
|
||||
|
||||
println!("Board hal init");
|
||||
let mut board: std::sync::MutexGuard<'_, PlantCtrlBoard<'_>> = BOARD_ACCESS.lock().unwrap();
|
||||
|
||||
let time = board.time();
|
||||
let mut cur = match time {
|
||||
Ok(cur) => cur,
|
||||
Err(err) => {
|
||||
log::error!("time error {}", err);
|
||||
DateTime::from_timestamp_millis(0).unwrap()
|
||||
}
|
||||
};
|
||||
let mut tank_driver = AdcDriver::new(peripherals.adc2, &adc_config).unwrap();
|
||||
let mut tank_channel: AdcChannelDriver<'_, { attenuation::DB_11 }, Gpio5> =
|
||||
AdcChannelDriver::new(peripherals.pins.gpio5).unwrap();
|
||||
//check if we know the time current > 2020
|
||||
if cur.year() < 2020 {
|
||||
//assume TZ safe times ;)
|
||||
cur = *cur.with_hour(15).get_or_insert(cur);
|
||||
|
||||
}
|
||||
|
||||
println!("cur is {}", cur);
|
||||
|
||||
|
||||
let mut online_mode: OnlineMode;
|
||||
|
||||
println!("attempting to connect wifi");
|
||||
let mut ssid: heapless::String<32> = heapless::String::new();
|
||||
let mut password: heapless::String<64> = heapless::String::new();
|
||||
|
||||
ssid.push_str("C3MA").unwrap();
|
||||
password.push_str("penis").unwrap();
|
||||
|
||||
let ip_address: Option<String>;
|
||||
match board.wifi(ssid, Option::Some(password), 10000) {
|
||||
Ok(ip_info) => {
|
||||
ip_address = Some(ip_info.ip.to_string());
|
||||
online_mode = OnlineMode::Wifi;
|
||||
}
|
||||
Err(_) => {
|
||||
panic!("wifi down");
|
||||
}
|
||||
}
|
||||
|
||||
if online_mode == OnlineMode::Wifi {
|
||||
match board.sntp(1000 * 5) {
|
||||
Ok(new_time) => {
|
||||
cur = new_time;
|
||||
online_mode = OnlineMode::SnTp;
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Sntp error {}", err);
|
||||
online_mode = OnlineMode::SnTp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("Running logic at utc {}", cur);
|
||||
let europe_time = cur.with_timezone(&Berlin);
|
||||
println!("Running logic at europe/berlin {}", europe_time);
|
||||
|
||||
//do mqtt before config check, as mqtt might configure
|
||||
if online_mode == OnlineMode::SnTp {
|
||||
match board.mqtt() {
|
||||
Ok(_) => {
|
||||
println!("Mqtt connection ready");
|
||||
online_mode = OnlineMode::Online;
|
||||
}
|
||||
Err(err) => {
|
||||
panic!("Could not connect mqtt due to {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if online_mode == OnlineMode::Online {
|
||||
match ip_address {
|
||||
Some(add_some) => {
|
||||
let _ = board.mqtt_publish("/firmware/address", add_some.as_bytes());
|
||||
}
|
||||
None => {
|
||||
let _ = board.mqtt_publish("/firmware/address", "N/A?".as_bytes());
|
||||
}
|
||||
}
|
||||
let _ = board.mqtt_publish(
|
||||
"/firmware/last_online",
|
||||
europe_time.to_rfc3339().as_bytes(),
|
||||
);
|
||||
let _ = board.mqtt_publish("/state", "online".as_bytes());
|
||||
|
||||
}
|
||||
drop(board);
|
||||
|
||||
loop {
|
||||
|
||||
let mut delay = Delay::new_default();
|
||||
|
||||
|
||||
in1.set_low().unwrap();
|
||||
in2.set_high().unwrap();
|
||||
for i in 0..50 {
|
||||
delay.delay_ms(100);
|
||||
let value_up = tank_driver.read(&mut tank_channel).unwrap();
|
||||
println!("up {}", value_up);
|
||||
}
|
||||
|
||||
let value_down = tank_driver.read(&mut tank_channel).unwrap();
|
||||
println!("down {}", value_down);
|
||||
in1.set_low().unwrap();
|
||||
in2.set_low().unwrap();
|
||||
delay.delay_ms(5000);
|
||||
//wait till earth ends
|
||||
Delay::new(1000).delay_ms(1000);
|
||||
let mut lock = BOARD_ACCESS.lock().unwrap();
|
||||
let position = lock.get_position();
|
||||
lock.mqtt_publish("/position", format!("{}", position).as_bytes()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let result = safe_main();
|
||||
match result {
|
||||
Ok(_) => {
|
||||
println!("Main app finished, restarting");
|
||||
unsafe { esp_restart() };
|
||||
}
|
||||
Err(err) => {
|
||||
panic!("Failed main {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
//error codes
|
||||
//error_reading_config_after_upgrade
|
||||
//error_no_config_after_upgrade
|
||||
//error_tank_sensor_fault
|
||||
|
3
tuer/.gitignore
vendored
Normal file
3
tuer/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
*.gbr
|
||||
*.ngc
|
||||
*.png
|
11
tuer/gerber/generatePCB.sh
Executable file
11
tuer/gerber/generatePCB.sh
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
# Needs the tool pcb2gcode
|
||||
# Was documented at: http://marcuswolschon.blogspot.de/2013/02/milling-pcbs-using-gerber2gcode.html
|
||||
|
||||
MILLSPEED=600
|
||||
MILLFEED=200
|
||||
PROJECT=tuer
|
||||
pcb2gcode --metric --back $PROJECT-B_Cu.gbr --cutter-diameter 0.2 --zsafe 5 --zchange 10 --zwork -0.01 --offset 0.02 --mill-feed $MILLFEED --mill-speed $MILLSPEED --drill $PROJECT.drl --zdrill -2.5 --drill-feed $MILLFEED --drill-speed $MILLSPEED --basename $PROJECT
|
||||
# one-drill parameter workaround
|
||||
sed ${PROJECT}_drill.ngc -e "s/^T[0-9]$/T1/g" > ${PROJECT}_drillOne.ngc
|
||||
mv ${PROJECT}_drillOne.ngc ${PROJECT}_drill.ngc
|
6571
tuer/tuer.kicad_pcb
6571
tuer/tuer.kicad_pcb
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"board": {
|
||||
"active_layer": 31,
|
||||
"active_layer": 36,
|
||||
"active_layer_preset": "All Layers",
|
||||
"auto_track_width": false,
|
||||
"hidden_netclasses": [],
|
||||
|
@@ -130,7 +130,7 @@
|
||||
"min_track_width": 0.0,
|
||||
"min_via_annular_width": 0.1,
|
||||
"min_via_diameter": 0.5,
|
||||
"solder_mask_to_copper_clearance": 0.0,
|
||||
"solder_mask_to_copper_clearance": 0.005,
|
||||
"use_height_for_length_calcs": true
|
||||
},
|
||||
"teardrop_options": [
|
||||
|
@@ -5,6 +5,108 @@
|
||||
(uuid "7fb6a37b-681a-4032-8cf4-e22d21292861")
|
||||
(paper "A4")
|
||||
(lib_symbols
|
||||
(symbol "Connector:TestPoint"
|
||||
(pin_numbers hide)
|
||||
(pin_names
|
||||
(offset 0.762) hide)
|
||||
(exclude_from_sim no)
|
||||
(in_bom yes)
|
||||
(on_board yes)
|
||||
(property "Reference" "TP"
|
||||
(at 0 6.858 0)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
)
|
||||
)
|
||||
(property "Value" "TestPoint"
|
||||
(at 0 5.08 0)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
)
|
||||
)
|
||||
(property "Footprint" ""
|
||||
(at 5.08 0 0)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
(hide yes)
|
||||
)
|
||||
)
|
||||
(property "Datasheet" "~"
|
||||
(at 5.08 0 0)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
(hide yes)
|
||||
)
|
||||
)
|
||||
(property "Description" "test point"
|
||||
(at 0 0 0)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
(hide yes)
|
||||
)
|
||||
)
|
||||
(property "ki_keywords" "test point tp"
|
||||
(at 0 0 0)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
(hide yes)
|
||||
)
|
||||
)
|
||||
(property "ki_fp_filters" "Pin* Test*"
|
||||
(at 0 0 0)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
(hide yes)
|
||||
)
|
||||
)
|
||||
(symbol "TestPoint_0_1"
|
||||
(circle
|
||||
(center 0 3.302)
|
||||
(radius 0.762)
|
||||
(stroke
|
||||
(width 0)
|
||||
(type default)
|
||||
)
|
||||
(fill
|
||||
(type none)
|
||||
)
|
||||
)
|
||||
)
|
||||
(symbol "TestPoint_1_1"
|
||||
(pin passive line
|
||||
(at 0 0 90)
|
||||
(length 2.54)
|
||||
(name "1"
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
)
|
||||
)
|
||||
(number "1"
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(symbol "Connector_Generic:Conn_01x02"
|
||||
(pin_names
|
||||
(offset 1.016) hide)
|
||||
@@ -2560,6 +2662,28 @@
|
||||
)
|
||||
)
|
||||
)
|
||||
(global_label "FAULT"
|
||||
(shape input)
|
||||
(at 82.55 105.41 90)
|
||||
(fields_autoplaced yes)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
(justify left)
|
||||
)
|
||||
(uuid "f791b88d-4964-4fd1-9323-a805d5e72a2d")
|
||||
(property "Intersheetrefs" "${INTERSHEET_REFS}"
|
||||
(at 82.55 96.9214 90)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
(justify left)
|
||||
(hide yes)
|
||||
)
|
||||
)
|
||||
)
|
||||
(global_label "GND"
|
||||
(shape input)
|
||||
(at 96.52 38.1 0)
|
||||
@@ -3136,6 +3260,73 @@
|
||||
)
|
||||
)
|
||||
)
|
||||
(symbol
|
||||
(lib_id "Connector:TestPoint")
|
||||
(at 82.55 105.41 180)
|
||||
(unit 1)
|
||||
(exclude_from_sim no)
|
||||
(in_bom yes)
|
||||
(on_board yes)
|
||||
(dnp no)
|
||||
(fields_autoplaced yes)
|
||||
(uuid "a551a88f-1612-4388-9628-3e64b8556d8d")
|
||||
(property "Reference" "TP1"
|
||||
(at 85.09 107.4419 0)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
(justify right)
|
||||
)
|
||||
)
|
||||
(property "Value" "TestPoint"
|
||||
(at 85.09 109.9819 0)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
(justify right)
|
||||
)
|
||||
)
|
||||
(property "Footprint" "TestPoint:TestPoint_Pad_2.5x2.5mm"
|
||||
(at 77.47 105.41 0)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
(hide yes)
|
||||
)
|
||||
)
|
||||
(property "Datasheet" "~"
|
||||
(at 77.47 105.41 0)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
(hide yes)
|
||||
)
|
||||
)
|
||||
(property "Description" "test point"
|
||||
(at 82.55 105.41 0)
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
)
|
||||
(hide yes)
|
||||
)
|
||||
)
|
||||
(pin "1"
|
||||
(uuid "36a5a36f-7d5c-4040-9dc3-c4580ccae7b0")
|
||||
)
|
||||
(instances
|
||||
(project "tuer"
|
||||
(path "/7fb6a37b-681a-4032-8cf4-e22d21292861"
|
||||
(reference "TP1")
|
||||
(unit 1)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(symbol
|
||||
(lib_id "Device:C_Polarized")
|
||||
(at 115.57 68.58 0)
|
||||
|
Reference in New Issue
Block a user