Skip to content

Commit

Permalink
Libhal 2.0 port science (#114)
Browse files Browse the repository at this point in the history
* 🚧 working on porting to 2.0, started base file structure

* started porting to libhal prebuilts

* working on port

* Nearly finished up the init platform. Will work on mc next

* ✨ finished the initalizing platform

* ✨ 🚧 finished converting all the basic logic to libhal2.0

* :constructions: nealy finished mission control implementation

* 🚧 finished adding mission control just need to fix errors

* This is before I switch all of the pairs to using pointers

* ✨ finished switching the servos over to offset homing

* ✨ finished changing from shared pointers to regular pointers, then allowing for multiple legs to be used in the control loop

* updated the mission control

* ✨ changed homing function to be a template function so it no longer needs pairs when passing in legs.

* ✨ switched away from int to size_t

* ✨ finished switching from templates

* ✨updated code to use homing structs instead of passing in seperate magnets, homed booleans, and servos while matching them

* 🐛finished testing and slight bug fixing

* fixed arm to build succesfully

* untested stuff to get commands

* fixing bugs drive esp

* :bandaid: fixed project name in cmake

* 🚧 finished testing for the day

* Fixed main part of code

* found issue with version numbers

* 🚧 ✨ got code to cycle through but the floats are outputting weird numbers. Need to test in person

* ✨ reverted back to using hardware and not print implementations

* ✨ added floating point support in cmake

* builds lmao

* Parsing commands currently works, just need to work out a bug which closes the connection

* buildable

* .

* angles change

* bug found where subtracting in offset servo instead of adding

* debugging offsets with servos

* changed order of legs in drive

* 🚧 finished testing for the day

* Fixed main part of code

* .

* found issue with version numbers

* bug found where subtracting in offset servo instead of adding

* 🚧 ✨ got code to cycle through but the floats are outputting weird numbers. Need to test in person

* tmp

* ✨ added floating point support in cmake

* building debug on ubuntu22.04

* ✨ reverted back to using hardware and not print implementations

* changed max rmp to 2

* Parsing commands currently works, just need to work out a bug which closes the connection

* esp not updating

* 10/16

* added static to socket config in lpc

* 10/17

* 10/17

* esp works now

* fuck lerp

* 👽 Libhal 2.0 update for arm

- Arm switched from drc to mc_x
- updated esp8266_mission_control.cpp to maintain content length

* 👽 ported arms esp over to drives

* 🚧 fixed bugs, now need to revert parse commands

* ✨ brought in old drive parse commands and fixed it, code now compiles

* 🚑 Fixed homing

- Fixed homing issue
- Fixed parsing changes
- Imported arm esp algorithm

* ✨ fully finished converting arm and drive to libhal 2.0, arm was working but I slowed down the rate at which we querried the server and decreaded the delay in arm from 10->6ms

* 🔥 removed all of the old code

* beginning lpc4078 port

* target platform builds

* added platform-implementation files

* demos

* science on libhal 2.0 has been updated

* deleted one of the flatform files cuz not required

* scd40.hpp and scd40.cpp transfered

* transfer scd40.cpp

* transfer scd40.hpp

* fix no return for read of scd40

* It works, trust me

* finished scd40 file transfer

* git scd40.cpp indentaiton

* fix scd40 namespace

* add member variables for scd40

* fix  member variable names for scd40

* create starter demo

* scd40 demo

* get_co2 method

* get_RH method

* get_RH and get_temp

* Revert "get_co2 method"

This reverts commit 75b7bd0.

* ✨ update cmakelists to add sources

* scd40 demo

* print statments added to scd40_demo.cpp

* :rotatiing_light: fixed errors and added buffer reading

* ♻️ have get functions call the get buffer functions

* start color sensor

* Created sht21 driver skeleton

* Implemented sht21 driver

* correct where get_RH_buffer() reads in the buffer

* fix demo print and change to table

* fix conan libhal version

* remove call to removed method

* read returns a struct for data

* remove individual data read methods in scd40.hpp

* implementation of color sensor done, demo and testing requried

* values change but they don't seem accurate, validity of sensor needs to be tested through arduino

* Rename scd40 Adresses

* Reimplement scd40 stop

* Add scd40 Settting Addresses

* Fixed scd40_demo to use struct

* Add crc fuction in scd40.hpp

* Adapt CRC code from data sheet

* Finished basic setting functions have not tested set_settings

* added soil_sensor_demo to science demos cmake

* Created soil sensor demo

* Added untested warnings

* Fixed default parameters and scaling factors for sensor functions

* deleeted extra files and color sensor works kind of, will need to buy another sensor to cross reference

* Seprate scd40 crc method into generate and validate

* Chages finally committed

* Gave option to set some of the settings

* Create blank perstaltic-pump.cpp & .hpp

* data type of settings and read_data channged from double to float

* Created Light Strip Driver

* Create led strip driver

* Updated driver with lights array seperated

---------

Co-authored-by: Coreyboy1820 <[email protected]>
Co-authored-by: corey <[email protected]>
Co-authored-by: FluffyFTW <[email protected]>
Co-authored-by: Kirthika Ashokkumar <[email protected]>
Co-authored-by: MichaelYKersey <[email protected]>
Co-authored-by: Andrew Lin <[email protected]>
  • Loading branch information
7 people authored Jan 30, 2024
1 parent c6e2085 commit 474c4cf
Show file tree
Hide file tree
Showing 31 changed files with 1,711 additions and 153 deletions.
19 changes: 19 additions & 0 deletions code/science/implementations/scd40.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <libhal-util/i2c.hpp>
#include <libhal-util/serial.hpp>
#include "scd40.hpp"

scd40::create(hal::i2c& p_i2c, hal::steady_clock& clock){
scd40 scd40 = scd40(p_i2c, clock);
HAL_CHECK(scd40.start())

}

scd40::start(){}

scd40::get_CO2{}

scd40::get_RH(){}

scd40::get_temp(){}
22 changes: 22 additions & 0 deletions drive/implementations/command_lerper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "../dto/drive.hpp"

namespace sjsu::drive {
class command_lerper
{
public:
int lerp(int speed)
{
previous_speed =
static_cast<int>(std::lerp(static_cast<float>(previous_speed),
static_cast<float>(speed),
speed_lerp));
return previous_speed;
}

private:
static constexpr float speed_lerp = 0.1f;
float previous_speed = 0; // don't touch mode or wheel orientation logic
};
} // namespace Drive
66 changes: 66 additions & 0 deletions drive/implementations/mode_switcher.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

#include "../dto/drive.hpp"
#include "../dto/motor_feedback.hpp"
#include "../platform-implementations/mission_control.hpp"

namespace sjsu::drive {
class mode_switch
{
public:
// Should handle all the logic for switching to new steer mode
mission_control::mc_commands switch_steer_mode(
mission_control::mc_commands commands,
tri_wheel_router_arguments previous_arguments,
motor_feedback current_motor_feedback,
hal::serial& terminal)
{
bool hubs_stopped((previous_arguments.left.speed >= -0.01f &&
previous_arguments.left.speed <= 0.01f) &&
(previous_arguments.right.speed >= -0.01f &&
previous_arguments.right.speed <= 0.01f) &&
(previous_arguments.back.speed >= -0.01f &&
previous_arguments.back.speed <= 0.01f));

bool steers_stopped((current_motor_feedback.left_steer_speed >= -0.01f &&
current_motor_feedback.left_steer_speed <= 0.01f) &&
(current_motor_feedback.right_steer_speed >= -0.01f &&
current_motor_feedback.right_steer_speed <= 0.01f) &&
(current_motor_feedback.back_steer_speed >= -0.01f &&
current_motor_feedback.back_steer_speed <= 0.01f));

if (previous_mode_ != commands.mode) {
in_the_middle_of_switching_modes_ = true;
skip_once_ = true;
}

if (in_the_middle_of_switching_modes_) {
commands.speed = 0;
if (!hubs_stopped) {
commands.mode = previous_mode_;
return commands;
} // hubs must be stopped to pass here

else if (!skip_once_ && steers_stopped) {
in_the_middle_of_switching_modes_ = false;
skip_once_ = true;
} // only once steer motors have stopped moving after hubs stopped will
// we exit switching modes
previous_mode_ = commands.mode;
skip_once_ = false;
}

return commands;
}

private:

char previous_mode_ =
'H'; // This is H for homing when the rover turns on, it makes sure that we
// don't switch to drive mode and allow commands to be parsed through
bool in_the_middle_of_switching_modes_ = true;
bool skip_once_ = true;

};

} // namespace Drive
22 changes: 22 additions & 0 deletions drive/implementations/serial.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <libhal/serial.hpp>

#include <string_view>

namespace Drive {
inline hal::result<std::string_view> get_commands(hal::serial& terminal)
{
std::array<hal::byte, 8192> buffer{};
auto received = HAL_CHECK(terminal.read(buffer)).data;
if (received.size() != 0) {
auto response = std::string_view(
reinterpret_cast<const char*>(received.data()), received.size());
hal::print<200>(
terminal, "%.*s", static_cast<int>(response.size()), response.data());
return response;
} else {
return std::string_view("");
}
}
} // namespace Drive
25 changes: 9 additions & 16 deletions science/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.20)

project(starter_project.elf VERSION 0.0.1 LANGUAGES CXX)
project(science VERSION 0.0.1 LANGUAGES CXX)

set(platform_library $ENV{LIBHAL_PLATFORM_LIBRARY})
set(platform $ENV{LIBHAL_PLATFORM})
Expand All @@ -26,15 +13,21 @@ endif()

find_package(libhal-${platform_library} REQUIRED CONFIG)
find_package(libhal-util REQUIRED CONFIG)
find_package(libhal-esp8266 REQUIRED CONFIG)

add_executable(${PROJECT_NAME}
main.cpp
application.cpp
applications/application.cpp
platforms/${platform}.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC .)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
target_link_options(${PROJECT_NAME} PRIVATE
--oslib=semihost --crt0=minimal)
target_link_libraries(${PROJECT_NAME} PRIVATE
libhal::${platform_library}
libhal::util)
libhal::util
libhal::esp8266)

# target_link_options(${PROJECT_NAME} INTERFACE "LINKER:--no-warn-rwx-segments")
libhal_post_build(${PROJECT_NAME})

26 changes: 0 additions & 26 deletions science/application.cpp

This file was deleted.

34 changes: 0 additions & 34 deletions science/application.hpp

This file was deleted.

6 changes: 4 additions & 2 deletions science/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ def build_requirements(self):

def requirements(self):
# Application requirements
self.requires("libhal-util/[^3.0.0]")
self.requires("libhal/[^2.0.3]")
self.requires("libhal-util/[^3.0.1]")
self.requires("libhal-esp8266/[^2.0.1]")

# List of supported platforms
if str(self.options.platform).startswith("lpc40"):
self.requires("libhal-lpc40/[^2.0.0]")
self.requires("libhal-lpc40/[^2.1.2]")
else:
raise ConanInvalidConfiguration(
f"The platform '{str(self.options.platform)}' is not"
Expand Down
9 changes: 8 additions & 1 deletion science/demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ libhal_build_demos(
DEMOS
co2_demo
pressure_sensor_demo

soil_sensor_demo
scd40_demo
color_sensor_demo
led_strip_demo

SOURCES
../platform-implementations/scd40.cpp
../platform-implementations/pressure_sensor_bme680.cpp
../platform-implementations/soil_sensor_sht21.cpp
../platform-implementations/color_sensor_opt4048.cpp

PACKAGES
libhal-lpc40
Expand Down
33 changes: 33 additions & 0 deletions science/demos/applications/co2_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <libhal-armcortex/dwt_counter.hpp>
#include <libhal-armcortex/startup.hpp>
#include <libhal-armcortex/system_control.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/units.hpp>

#include "../../platform-implementations/co2_sensor_sc40.cpp"
#include "../hardware_map.hpp"

using namespace hal::literals;
using namespace std::chrono_literals;
namespace sjsu::science {

hal::status application(application_framework& p_framework)
{
// configure drivers
auto& i2c2 = *p_framework.i2c;
auto& clock = *p_framework.steady_clock;
auto& terminal = *p_framework.terminal;

auto co2_sensor = HAL_CHECK(science::co2_sensor::create(i2c2, clock));

while (true) {
hal::delay(clock, 500ms);
auto co2_level = HAL_CHECK(co2_sensor.read_co2());
hal::print<64>(terminal, "C02: %d\n", co2_level);
hal::delay(clock, 1ms);
}

return hal::success();
}
} // namespace sjsu::science
54 changes: 54 additions & 0 deletions science/demos/applications/color_sensor_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <libhal-armcortex/dwt_counter.hpp>
#include <libhal-armcortex/startup.hpp>
#include <libhal-armcortex/system_control.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/units.hpp>

#include "../../platform-implementations/color_sensor_opt4048.hpp"
#include "../hardware_map.hpp"

using namespace hal::literals;
using namespace std::chrono_literals;

hal::status probe_bus(hal::i2c& i2c, hal::serial& console) {
hal::print(console, "\n\nProbing i2c2\n");
for(hal::byte addr = 0x08; addr < 0x78; addr++) {
if (hal::probe(i2c, addr)) {
hal::print<8>(console, "0x%02X ", addr);
}else{
hal::print(console, " -- ");
}
if(addr % 8 == 7) {
hal::print(console, "\n");
}
}
hal::print(console, "\n");

return hal::success();
}

namespace sjsu::science {

hal::status application(application_framework& p_framework)
{
// configure drivers
auto& i2c2 = *p_framework.i2c;
auto& clock = *p_framework.steady_clock;
auto& terminal = *p_framework.terminal;

hal::print(terminal, "hi\n");
HAL_CHECK(probe_bus(i2c2, terminal));

//create the stuff here
auto color_sensor = HAL_CHECK(science::opt4048::create(i2c2, clock, terminal));
while(true){
hal::print(terminal, "\nnew measurement\n");
auto readings = HAL_CHECK(color_sensor.get_data());
hal::print<40>(terminal, "R: %f\t G: %f\t B: %f\n", readings.r, readings.g, readings.b);
hal::delay(clock, 50ms);
}

return hal::success();
}
} // namespace sjsu::science
Loading

0 comments on commit 474c4cf

Please sign in to comment.