Skip to content
Open

MVP #22

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ target_sources(app PRIVATE
src/drive.c
src/dig.c
src/crsf.c
src/place.c
src/tamp.c
#src/imu.c # not in use
src/led_strip.c
src/led_controller.c
Expand Down
115 changes: 57 additions & 58 deletions app/esp32s3_devkitc_procpu.overlay
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
/*
* ESP32-S3 DevKitC Motor Driver Overlay
* ESP32-S3 DevKitC Hardware Overlay
*
* MD20A (PWM) motors:
* driveRight — DIR: GPIO1 PWM: GPIO4 (LEDC ch0)
* driveLeft — DIR: GPIO2 PWM: GPIO5 (LEDC ch1)
* digActuator — DIR: GPIO3 PWM: GPIO6 (LEDC ch2)
* driveRight/NavM2 — DIR: GPIO3 PWM: GPIO9 (LEDC ch0)
* driveLeft/Navm1 — DIR: GPIO37 PWM: GPIO38 (LEDC ch1)
* digActuator/LinAct1 — DIR: GPIO14 PWM: GPIO21 (LEDC ch2)
*
* DM542T (stepper) motors:
* digStepper — DIR: GPIO14 PUL: GPIO13
* susanStepper — DIR: GPIO16 PUL: GPIO15
* tampActuator — DIR: GPIO18 PUL: GPIO17
* digStepper — DIR: GPIO48 PUL: GPIO47
* susanStepper — DIR: GPIO11 PUL: GPIO10
* tampActuator — DIR: GPIO13 PUL: GPIO12
*
* Servo motors:
* soilDrop — PWM: GPIO7
* soilDrop — PWM: GPIO8
*
* BQ76930 BMS:
* I2C SDA: GPIO36 SCL: GPIO35
* ALERT: GPIO2 (active-low, open-drain — pull-up configured in driver)
*
* CRSF RC receiver:
* TX: GPIO18 RX: GPIO17 (UART1, 420000 baud)
*
* WS2812B LED strip:
* MOSI: GPIO5 (SPI2)
*/

#include <zephyr/dt-bindings/pinctrl/esp32s3-pinctrl.h>

/* -----------------------------------------------------------------------
* Pin control — route LEDC channels to physical GPIO pins
* Pin control — all pinmux groups must live here in &pinctrl
* ----------------------------------------------------------------------- */
&pinctrl {

/* LEDC PWM outputs */
ledc0_default: ledc0_default {
group1 {
pinmux = <LEDC_CH0_GPIO4>,
<LEDC_CH1_GPIO5>,
<LEDC_CH2_GPIO6>,
<LEDC_CH3_GPIO7>;
pinmux = <LEDC_CH0_GPIO9>,
<LEDC_CH1_GPIO38>,
<LEDC_CH2_GPIO21>,
<LEDC_CH3_GPIO8>;
output-enable;
};
};

// CRSF serial, could be different pin configs and uart channels on ESP32-S3
/* CRSF serial (UART1) */
uart1_crsf: uart1_crsf {
group1 {
pinmux = <UART1_TX_GPIO18>;
Expand All @@ -42,16 +54,8 @@
input-enable;
};
};
/* ← lsm6dso does NOT belong here */
};

/* lsm6dso goes here instead */
&i2c0 {
status = "okay";
pinctrl-0 = <&i2c0_default>;
pinctrl-names = "default";
clock-frequency = <I2C_BITRATE_FAST>;

/* I2C0 — BQ76930 BMS */
i2c0_default: i2c0_default {
group1 {
pinmux = <I2C0_SDA_GPIO36>,
Expand All @@ -61,7 +65,7 @@
};
};

// spi for led strip data pin control
/* SPI2 — WS2812B LED strip data line */
spim2_default: spim2_default {
group1 {
pinmux = <SPIM2_MOSI_GPIO5>;
Expand All @@ -70,32 +74,28 @@
};
};

&i2c0 {
/* -----------------------------------------------------------------------
* UART1 — CRSF RC receiver at 420 kbaud
* ----------------------------------------------------------------------- */
&uart1 {
status = "okay";
pinctrl-0 = <&i2c0_default>;
current-speed = <420000>;
pinctrl-0 = <&uart1_crsf>;
pinctrl-names = "default";
clock-frequency = <I2C_BITRATE_FAST>;

/*
lsm6dso: lsm6dso@6b {
compatible = "st,lsm6dso";
reg = <0x6b>;
status = "okay";
};
*/
};


// CRSF serial, baud rate at 420000
&uart1 {
/* -----------------------------------------------------------------------
* I2C0 — BQ76930 battery management IC
* ----------------------------------------------------------------------- */
&i2c0 {
status = "okay";
current-speed = <420000>;
pinctrl-0 = <&uart1_crsf>;
clock-frequency = <I2C_BITRATE_FAST>;
pinctrl-0 = <&i2c0_default>;
pinctrl-names = "default";
};

/* -----------------------------------------------------------------------
* LEDC (PWM) controller — 4 channels, 1 kHz, 10-bit resolution
* LEDC (PWM) — 4 channels
* ----------------------------------------------------------------------- */
&ledc0 {
status = "okay";
Expand All @@ -104,56 +104,55 @@
#address-cells = <1>;
#size-cells = <0>;

/* Timer 0 shared by all three channels */
channel0@0 {
reg = <0>;
timer = <0>;
};

channel1@1 {
reg = <1>;
timer = <0>;
};

channel2@2 {
reg = <2>;
timer = <0>;
};

channel3@3 {
reg = <3>;
timer = <1>;
};
};

// SPI controller for WS2812B LED strip
// hopefully this works, timing is controlled by the one and zero frame parameters, if anything goes wrong it might be that
/* -----------------------------------------------------------------------
* SPI2 — WS2812B LED strip
* ----------------------------------------------------------------------- */
&spi2 {
status = "okay";
pinctrl-0 = <&spim2_default>;
pinctrl-names = "default";

led_strip: ws2812@0 {
compatible = "worldsemi,ws2812-spi";
reg = <0>;
spi-max-frequency = <3200000>; /* 3.2MHz for better timing */
chain-length = <9>;
spi-max-frequency = <3200000>;
chain-length = <8>;
color-mapping = <2 /* GREEN */
1 /* RED */
3>; /* BLUE */
spi-one-frame = <0xFC>; /* 11111100 - longer '1' */
spi-zero-frame = <0x80>; /* 10000000 - shorter '0' */
1 /* RED */
3>; /* BLUE */
spi-one-frame = <0xFC>;
spi-zero-frame = <0x80>;
bits-per-symbol = <8>;
reset-delay = <280>;
label = "WS2812B";
};
};

/* -----------------------------------------------------------------------
* GPIO0 — already defined by the SoC; just ensure it is enabled.
* All dir/pulse pins are driven purely from software (gpio_pin_configure_dt),
* so no additional pinctrl entry is needed for plain GPIO on ESP32-S3.
* GPIO0 — direction/pulse pins driven from software
* ----------------------------------------------------------------------- */
&gpio0 {
status = "okay";
};
};

&gpio1 {
status = "okay";
};
6 changes: 6 additions & 0 deletions app/include/dig.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#ifndef DIG_H
#define DIG_H
#include "types.h"
#include "place.h"

#define DIG_LOWER_TIME_MS 2000
#define POST_DIG_DRIVE_TIME_MS 5000

int dig(mcu_t *mcu);
int move_dig_motor(mcu_t *mcu);
int post_dig_drive(mcu_t *mcu);


#endif // DIG_H
32 changes: 0 additions & 32 deletions app/include/imu.h

This file was deleted.

59 changes: 19 additions & 40 deletions app/include/led_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,39 @@

#include <stdint.h>
#include <stdbool.h>
#include "types.h"

/**
* Planting step status
* Initialize the LED controller and start the update thread.
* @param mcu pointer to the shared MCU state (reads mcu->state each cycle)
*/
enum planting_step_status {
PLANTING_NOT_STARTED, // White (default)
PLANTING_IN_PROGRESS, // Blinking white
PLANTING_COMPLETED, // Solid green
PLANTING_MAINTENANCE // Red
};
int led_controller_init(mcu_t *mcu);

/**
* System state
*/
enum system_state {
SYSTEM_IDLE, // Default state, planting LEDs white
SYSTEM_LOADING, // All planting LEDs breathe white
SYSTEM_PLANTING // In planting cycle
};

/**
* Initialize the LED controller
* This starts a background thread that updates LEDs based on state
*/
int led_controller_init(void);

/**
* Set battery percentage and charging state
* @param percentage: 0-100
* @param is_charging: true if charging, false otherwise
* Set battery percentage and charging state.
* Call this when BMS data is available.
* @param percentage 0-100
* @param is_charging true if currently charging
*/
void led_controller_set_battery(uint8_t percentage, bool is_charging);

/**
* Set the status of a specific planting step
* @param step: 1-4 (which planting step)
* @param status: Status of that step
*/
void led_controller_set_planting_step(uint8_t step, enum planting_step_status status);

/**
* Set system state
* @param state: System state enum
* Mark a planting step as having an error (turns that LED red).
* @param step 1-4 (1=Drive, 2=Dig, 3=Place, 4=Tamp)
* @param error true to set error, false to clear it
*/
void led_controller_set_system_state(enum system_state state);
void led_controller_set_planting_error(uint8_t step, bool error);

/**
* Complete the planting cycle (reset all to white)
* Signal that the full planting cycle is complete.
* All four planting LEDs will go solid white.
*/
void led_controller_complete_cycle(void);
void led_controller_set_planting_complete(void);

/**
* Get current states (for debugging)
* Reset planting state (clears complete flag and all errors).
* Call at the start of a new planting cycle.
*/
void led_controller_get_status(void);
void led_controller_reset_planting(void);

#endif
#endif
4 changes: 2 additions & 2 deletions app/include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include "types.h"

/* Stack size for the motor control thread */
#define MOTOR_THREAD_STACK_SIZE 2048
#define MAIN_THREAD_STACK_SIZE 4096

/* Priority for the motor control thread (cooperative, below main) */
#define MOTOR_THREAD_PRIORITY 5
#define MAIN_THREAD_PRIORITY 5

void rc_callback(const crsf_channels_t *ch);

Expand Down
4 changes: 4 additions & 0 deletions app/include/motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
#include "types.h"
#include "drive.h"

#define PUL_PER_REV 800

/* API */
int init_motor(mcu_t *mcu);
int move_motor(mcu_t *mcu);
int set_motor_speed(motor_t *motor, motor_direction_t dir, uint8_t speed_pct);
int set_step_motor(motor_t *motor, uint32_t degrees, uint32_t rpm, motor_direction_t direction);
int step_motor(motor_t *motor, uint32_t steps, uint32_t step_period_us, motor_direction_t direction);
int all_motors_off(mcu_t *mcu);

#endif
14 changes: 14 additions & 0 deletions app/include/place.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef PLACE_H
#define PLACE_H
#include "types.h"
#include "motor.h"
#include "servo.h"

#define SOIL_TIME_OPEN_MS 5000 //TODO: Figure out value

int place(mcu_t *mcu);
int place_plant(mcu_t *mcu);
int place_dirt(mcu_t *mcu);


#endif //PLACE_H
Loading