Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
127 commits
Select commit Hold shift + click to select a range
9e279c9
Moving files to keep basic interface cleaner
marcinpaluch1994 Apr 1, 2025
87d1a27
New cost function. Does not work on simulated cartpole, but works wel…
marcinpaluch1994 Apr 3, 2025
bf198c6
New cost function. Does not work on simulated cartpole, but works wel…
marcinpaluch1994 Apr 3, 2025
19ccf2f
Merge remote-tracking branch 'origin/Experiment_3_04_2025' into Exper…
marcinpaluch1994 Apr 3, 2025
5b47d27
Getting ready for experiments.
marcinpaluch1994 Apr 3, 2025
f9e0ef4
Getting ready for experiment run. No data collection, just new contro…
marcinpaluch1994 Apr 3, 2025
a0d4e37
UpsamplingScript
marcinpaluch1994 Apr 8, 2025
200c61d
UpsamplingScript
marcinpaluch1994 Apr 8, 2025
e95c402
Adding convertion of GRU to C code, not tested yet
marcinpaluch1994 Apr 11, 2025
880089d
Removing files from original location made optional.
marcinpaluch1994 Apr 13, 2025
ba5a9fd
Production version of script used to upsample CPP MPC data.
marcinpaluch1994 Apr 13, 2025
baf1b4a
More debugging info
marcinpaluch1994 Apr 13, 2025
c5adf7d
Noise generation updated adding option of timewise correlated noise.
marcinpaluch1994 Apr 13, 2025
adde5ec
Loss type settable from config.
marcinpaluch1994 Apr 14, 2025
beef5d8
Next step with neural network.
marcinpaluch1994 Apr 15, 2025
95e07ea
Next step and second derivatives possibly as network output
marcinpaluch1994 Apr 15, 2025
2d2f7fa
Subtract columns and calculate cartpole ODE along trajectories.
marcinpaluch1994 Apr 17, 2025
6ddf029
Merge remote-tracking branch 'origin/Experiment_3_04_2025' into Exper…
marcinpaluch1994 Apr 17, 2025
847b9b8
CPP now uses a header and title for CSV from CPS
marcinpaluch1994 Apr 23, 2025
2312734
calculate_cartpole_ode_along_trajectories now working
marcinpaluch1994 Apr 23, 2025
66e00a9
Pre-declare attributes of cartpole parameters so IDEs can see them
marcinpaluch1994 Apr 28, 2025
3c51a9f
Just cleaning
marcinpaluch1994 Apr 28, 2025
5a30834
Script for subtracting columns
marcinpaluch1994 Apr 28, 2025
85f9b36
Predictor with neural correction
marcinpaluch1994 Apr 28, 2025
87f7e87
Configs
marcinpaluch1994 Apr 28, 2025
0b73a93
New ODE Module, not yet properly tested.
marcinpaluch1994 Apr 29, 2025
0e28739
Corrections to enable training of ODE.
marcinpaluch1994 Apr 29, 2025
5d6a847
Bug, removing unused line causing circular import
marcinpaluch1994 Apr 29, 2025
cedc087
ODE Module augmented with neural correction close to working but not …
marcinpaluch1994 Apr 29, 2025
dc1fba4
Better default config
marcinpaluch1994 Apr 29, 2025
d501910
Correcting bugs
marcinpaluch1994 Apr 29, 2025
4c60c57
Removing duplicated functions
marcinpaluch1994 Apr 29, 2025
e012c12
Removing duplicated functions
marcinpaluch1994 Apr 29, 2025
da951af
different default for config
marcinpaluch1994 Apr 29, 2025
f8d2fcc
Merge pull request #64 from SensorsINI/Experiment_3_04_2025
marcinpaluch1994 Apr 29, 2025
c3dccb2
refs
marcinpaluch1994 Apr 29, 2025
d30b623
optimizer settings for rpgd better for physical cartpole
marcinpaluch1994 Apr 29, 2025
c16f90b
ODEModule converted into predictor trainer. Now it include warm up le…
marcinpaluch1994 Apr 30, 2025
9033181
New data preprocessing functions
marcinpaluch1994 May 2, 2025
b265b44
Separately setting activation regularization for the last network layer
marcinpaluch1994 May 2, 2025
07d8548
Merge remote-tracking branch 'origin/Development' into Development
marcinpaluch1994 May 4, 2025
5129049
Updating L4DC plots with option for poster version
marcinpaluch1994 Jun 3, 2025
caa96f8
Merge remote-tracking branch 'origin/Development' into Development
marcinpaluch1994 Jun 3, 2025
cd72d79
Paper plots updated
marcinpaluch1994 Jun 18, 2025
2184e08
Merge remote-tracking branch 'origin/master' into Development
marcinpaluch1994 Jun 18, 2025
8d96964
Correcting noise in noise generator and renaming variables.
marcinpaluch1994 Jun 18, 2025
28106a5
Preventing crash if pole length not in variable parameters.
marcinpaluch1994 Jun 18, 2025
8e9ff1e
Generating QR codes - used for L4DC poster
marcinpaluch1994 Jun 18, 2025
17e13cd
Renaming files now with an option for recursive search.
marcinpaluch1994 Jun 18, 2025
cb089dc
Adding instructions to README.md to create aliases for easier termina…
marcinpaluch1994 Jun 18, 2025
ac43dfa
Data augmentation back to numpy for better tensorflow training.
marcinpaluch1994 Jun 18, 2025
d5bc9b4
Removed unused variable
marcinpaluch1994 Jun 18, 2025
e1e68c2
New version including C-LSTM, not yet tested.
marcinpaluch1994 Jun 19, 2025
2c21e5d
self.hls4ml attribute replaced with nn_evaluator mode to prepare to r…
marcinpaluch1994 Jun 19, 2025
01c754b
Default to adaptive network
marcinpaluch1994 Jun 20, 2025
cd9f0e8
Printing additional message to facilitate debugging.
Florian-Bolli Jun 23, 2025
9789b34
Removing TF micro completely from the project. It works better with h…
marcinpaluch1994 Jun 24, 2025
5589e43
Merge remote-tracking branch 'origin/Development' into Development
marcinpaluch1994 Jun 24, 2025
cd98cb8
First Adaptive network in C format added.
marcinpaluch1994 Jun 25, 2025
1ff3a6d
Separate file evaluate train RL policy
marcinpaluch1994 Jul 7, 2025
f034073
Correction in comments
marcinpaluch1994 Jul 7, 2025
67dd878
Former RL training script now as minimal training example
marcinpaluch1994 Jul 7, 2025
3995c3a
Former RL training script now as minimal training and evaluation example
marcinpaluch1994 Jul 7, 2025
bf43a29
Making run basic training with SAC
marcinpaluch1994 Jul 7, 2025
f673fd1
Options for different cartpole models
marcinpaluch1994 Jul 7, 2025
a31dfb5
Cartpole_Sensors changed to CartpoleCustomSim
marcinpaluch1994 Jul 7, 2025
f49fafe
Moving files
marcinpaluch1994 Jul 7, 2025
39fb620
New structure of RL environment
marcinpaluch1994 Jul 7, 2025
4d6aa87
New structure of RL cartpole!
marcinpaluch1994 Jul 8, 2025
5a96826
Changing termination condition to terminate if pole falls after stabi…
marcinpaluch1994 Jul 8, 2025
2639611
Each run gets a dedicated folder
marcinpaluch1994 Jul 8, 2025
1856f35
Truncation signal with gymnasium wrapper
marcinpaluch1994 Jul 8, 2025
fa41ffe
Counting successful (reaching end of episode) episodes in the last 50…
marcinpaluch1994 Jul 8, 2025
9954582
More reasonable experiment length.
marcinpaluch1994 Jul 8, 2025
d9b9993
Changing default
marcinpaluch1994 Jul 8, 2025
36772c0
Small corrections
marcinpaluch1994 Jul 8, 2025
4ed64f6
Renaming variable, to make the script work
marcinpaluch1994 Jul 8, 2025
e113daf
starting point for swingup now down!
marcinpaluch1994 Jul 8, 2025
169202c
Removing redundant print statement
marcinpaluch1994 Jul 8, 2025
78565da
New variables
marcinpaluch1994 Jul 8, 2025
b1bd93d
Better definition of swing-up success.
marcinpaluch1994 Jul 8, 2025
8b41396
Changing code order in Evaluate_RL_Cartpole.py to start with visual i…
marcinpaluch1994 Jul 9, 2025
2192051
Changing code order in Evaluate_RL_Cartpole.py to start with visual i…
marcinpaluch1994 Jul 9, 2025
ffa5442
task and env type as part of the folder name with saved data
marcinpaluch1994 Jul 9, 2025
bfdb919
new default
marcinpaluch1994 Jul 9, 2025
93da1a0
redundant variables removal
marcinpaluch1994 Jul 9, 2025
11a2a41
angle_limit now defined in tasks as it is not a feature of environment.
marcinpaluch1994 Jul 9, 2025
a9b48ec
Random target generation added to tasks.py
marcinpaluch1994 Jul 9, 2025
385596d
More logging
marcinpaluch1994 Jul 9, 2025
9d2777c
Option to connect to remote cartpole - either simulated or physical v…
marcinpaluch1994 Jul 9, 2025
f7f2c94
Removing probably redundant reward part
marcinpaluch1994 Jul 9, 2025
7aed494
minimal needed config for "remote" controller
marcinpaluch1994 Jul 9, 2025
658fab8
Removing double reset
marcinpaluch1994 Jul 10, 2025
25ed8fe
and double rendering
marcinpaluch1994 Jul 10, 2025
b1eadd7
Debugging timeout
marcinpaluch1994 Jul 10, 2025
45dd4ea
Changes of order for clarity.
marcinpaluch1994 Jul 10, 2025
535014c
Missing field in config controller for remote controller
marcinpaluch1994 Jul 11, 2025
df848f7
Difflg initial commit
marcinpaluch1994 Jul 11, 2025
5390132
Test
marcinpaluch1994 Jul 12, 2025
f73cbc9
First version of communicating data from DVS to carpole main program.
marcinpaluch1994 Jul 12, 2025
ddc38ec
First version of cartpole_ekf. Not tested yet.
marcinpaluch1994 Jul 12, 2025
8610feb
EKF newest version
marcinpaluch1994 Jul 14, 2025
0f8ed47
Calculated, quality low
marcinpaluch1994 Jul 14, 2025
4ea6e9d
New version of EKF
marcinpaluch1994 Jul 14, 2025
b0cd4a9
Enabling EKF
marcinpaluch1994 Jul 16, 2025
e61e3d4
EKF now manually tunable
marcinpaluch1994 Jul 16, 2025
be92b0f
Settings to be able to connect FPGA controller
marcinpaluch1994 Jul 17, 2025
a3fff8e
Merge branch 'difflg' into Development
marcinpaluch1994 Jul 17, 2025
08cc110
Merge remote-tracking branch 'origin/difflg' into Development
marcinpaluch1994 Jul 17, 2025
9062ebc
Uploaded diff logic controller
czihao Jul 17, 2025
24390bb
Merge branch 'difflg' of https://github.com/SensorsINI/CartPoleSimula…
czihao Jul 17, 2025
9df8ffe
Merge remote-tracking branch 'origin/difflg' into Development
marcinpaluch1994 Jul 17, 2025
bb88b38
Adding script deriving cartpole equations via lagrange mechanics.
marcinpaluch1994 Aug 4, 2025
25158d5
Define names of input and output in predictor specification not in ge…
marcinpaluch1994 Aug 31, 2025
4b1e4da
Fixing the texbox from cartpole gui not being read.
marcinpaluch1994 Sep 1, 2025
a26a0f1
missing variable
marcinpaluch1994 Sep 10, 2025
d37dde0
First version for running C controllers on PC
marcinpaluch1994 Sep 11, 2025
dde7764
Making the Ops name specified in the config
marcinpaluch1994 Sep 11, 2025
773d82e
Making the Ops name specified in the config
marcinpaluch1994 Sep 11, 2025
f31c3ee
Refs.
marcinpaluch1994 Sep 11, 2025
5e8179b
Removing redundant parameters.
marcinpaluch1994 Sep 11, 2025
1836009
Merge remote-tracking branch 'origin/Development' into Development
marcinpaluch1994 Sep 11, 2025
a4dfe67
Renaming
marcinpaluch1994 Sep 17, 2025
08e2a31
removing redundant reconfiguration of the controller.
marcinpaluch1994 Sep 18, 2025
ee4c08d
Saving normalization vector to dedicated folder.
marcinpaluch1994 Sep 18, 2025
5df4476
bug in hls4ml
marcinpaluch1994 Sep 18, 2025
60dd953
Removing redundant plot
marcinpaluch1994 Sep 19, 2025
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
87 changes: 67 additions & 20 deletions CartPole/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@author: Marcin
"""
# Import module to save history of the simulation as csv file

import os
# Import module to interact with OS
import traceback
# Import module to get a current time and date used to name the files containing the history of simulations
Expand All @@ -25,7 +25,7 @@
from CartPole.cartpole_parameters import (J_fric, L, m_cart, M_fric, TrackHalfLength,
CP_PARAMETERS_DEFAULT,
g, k, m_pole, u_max, v_max,
controlBias, controlDisturbance, controlDisturbance_mode
controlNoiseBias, controlNoiseScale, controlNoise_mode, controlNoiseCorrelation,
)
# Interpolate function to create smooth random track
# Run range() automatically adding progress bar in terminal
Expand All @@ -36,13 +36,14 @@
from CartPole.latency_adder import LatencyAdder
from CartPole.load import get_full_paths_to_csvs, load_csv_recording
from CartPole.noise_adder import NoiseAdder
from CartPole.noise_control_signal import add_control_noise
from CartPole.noise_control_signal import ControlNoiseGenerator
from CartPole.random_target_generator import Generate_Random_Trace_Function
from CartPole.state_utilities import (ANGLE_COS_IDX, ANGLE_IDX, ANGLE_SIN_IDX,
ANGLED_IDX, POSITION_IDX, POSITIOND_IDX)
from CartPole.state_utilities import create_cartpole_state
from CartPole.summary_plots import summary_plots
from CartPole.controller_informer import ControllerInformer
from SI_Toolkit.Predictors.neural_network_evaluator import neural_network_evaluator

s0 = create_cartpole_state()

Expand Down Expand Up @@ -72,8 +73,6 @@
config = load_config("cartpole_physical_parameters.yml")
PATH_TO_EXPERIMENT_RECORDINGS_DEFAULT = config["cartpole"]["PATH_TO_EXPERIMENT_RECORDINGS_DEFAULT"]

rng = create_rng(__name__, config["cartpole"]["seed"])


class CartPole(EnvironmentBatched):
num_states = 6
Expand All @@ -84,6 +83,17 @@ def __init__(self, initial_state=s0, path_to_experiment_recordings=None, target_
self.config = config["cartpole"]
self.rng_CartPole = create_rng(self.__class__.__name__, self.config["seed"])

self.next_step_mode = self.config["next_step_mode"]

self.next_step_neural_model = None
if self.next_step_mode == 'NeuralModel':
self.next_step_neural_model_path = self.config['next_step_neural_model_path']
self.next_step_neural_model = neural_network_evaluator(
net_name=os.path.basename(self.next_step_neural_model_path),
path_to_models=os.path.dirname(self.next_step_neural_model_path),
batch_size=1,
)

self.slider = target_slider

self.time_L_last_change = None
Expand Down Expand Up @@ -138,6 +148,16 @@ def __init__(self, initial_state=s0, path_to_experiment_recordings=None, target_
self.NoiseAdderInstance = NoiseAdder()
self.s_with_noise_and_latency = np.copy(self.s)

self.control_noise_generator = ControlNoiseGenerator(
self.rng_CartPole,
controlNoise_Mode=self.config["controlNoise_mode"],
controlNoise_Scale=self.config["controlNoiseScale"],
controlNoise_Bias=self.config["controlNoiseBias"],
controlNoise_Correlation=self.config["controlNoiseCorrelation"],
dt=1.0, # Overwritten later
initial_state=0.0, # Overwritten later
)

self.vertical_angle_offset_updater = ParameterUpdater(self.config['vertical_angle_offset'])
self.vertical_angle_offset_init = np.deg2rad(self.config['vertical_angle_offset']['init_value'])
self.vertical_angle_offset = self.vertical_angle_offset_init
Expand Down Expand Up @@ -293,7 +313,7 @@ def update_state(self):
self.update_target_equilibrium()

# Calculate the next state
self.cartpole_integration()
self.cartpole_next_step()

# Calculate the correction to the state due to the bounce at the edge if applies
self.edge_bounce()
Expand All @@ -316,7 +336,7 @@ def update_state(self):
self.Q2u()

# Update second derivatives
self.cartpole_ode()
self.cartpole_second_derivatives()

if block_pole_at_90:
self.angleDD = 0.0
Expand All @@ -338,12 +358,15 @@ def add_noise_and_latency(self):
s_delayed = self.LatencyAdderInstance.get_interpolated_delayed_state()
self.s_with_noise_and_latency = self.NoiseAdderInstance.add_noise_to_measurement(s_delayed, copy=False)
self.s_with_noise_and_latency = self.update_vertical_angle_offset(self.s_with_noise_and_latency)

def cartpole_second_derivatives(self):
self.angleDD, self.positionDD = self.cpe.cartpole_second_derivatives(self.s, self.Q, L, m_pole, u_max)

def cartpole_ode(self):
self.angleDD, self.positionDD = self.cpe.cartpole_ode_interface(self.s, self.u, L=float(L), m_pole=float(m_pole))

def Q2u(self):
self.u = self.cpe.Q2u(self.Q)
self.u = self.cpe.Q2u(self.Q, u_max)

def update_vertical_angle_offset(self, s):
self.vertical_angle_offset = self.vertical_angle_offset_updater.update_parameter(
Expand Down Expand Up @@ -441,6 +464,30 @@ def save_csv_routine(self):

self.dt_save_steps_counter = 0


def cartpole_next_step(self):
if self.next_step_mode == 'Euler':
self.cartpole_integration()
elif self.next_step_mode == 'NeuralModel':
self.get_cartpole_next_step_from_neural_model()
else:
raise ValueError(f"Unknown next_step_mode: {self.next_step_mode}")

def get_cartpole_next_step_from_neural_model(self):
network_input = self.next_step_neural_model.compose_input(self.variables_to_log)
_ = self.next_step_neural_model.step(network_input)
net_output_dict = self.next_step_neural_model.net_output_dict

if 'angle' in net_output_dict:
self.s[ANGLE_IDX] = net_output_dict['angle']
elif 'angle_cos' in net_output_dict and 'angle_sin' in net_output_dict:
self.s[ANGLE_IDX] = np.arctan2(net_output_dict['angle_sin'], net_output_dict['angle_cos'])

self.s[ANGLED_IDX] = net_output_dict['angleD']
self.s[POSITION_IDX] = net_output_dict['position']
self.s[POSITIOND_IDX] = net_output_dict['positionD']


# A method integrating the cartpole ode over time step dt
# Currently we use a simple single step Euler stepping
def cartpole_integration(self):
Expand Down Expand Up @@ -488,8 +535,7 @@ def Update_Q(self):
raise AttributeError("Manual stabilization mode activated and no slider object created.")
self.Q_update_time = 0.0
else: # in this case slider gives a target position, lqr regulator
# self.Q_ccrc = add_control_noise(self.Q_calculated, rng,
# controlDisturbance_mode, controlDisturbance, controlBias)
# self.Q_ccrc = self.control_noise_generator.add_control_noise(self.Q_calculated)
self.Q_ccrc = self.Q_applied
update_start = timeit.default_timer()
self.L_for_controller = float(self.controller_informer.get_parameters(
Expand Down Expand Up @@ -520,8 +566,7 @@ def Update_Q(self):
))
self.Q_update_time = timeit.default_timer()-update_start

self.Q_applied = add_control_noise(self.Q_calculated, rng,
controlDisturbance_mode, controlDisturbance, controlBias)
self.Q_applied = self.control_noise_generator.add_control_noise(self.Q_calculated)

self.Q = self.Q_applied
self.dt_controller_steps_counter = 0
Expand Down Expand Up @@ -624,6 +669,8 @@ def setup_cartpole_random_experiment(self,
if keep_target_equilibrium_x_seconds_up is not None: self.keep_target_equilibrium_x_seconds_up = keep_target_equilibrium_x_seconds_up
if keep_target_equilibrium_x_seconds_down is not None: self.keep_target_equilibrium_x_seconds_down = keep_target_equilibrium_x_seconds_down

self.control_noise_generator.reset(noise_initial_state=0.0, dt=self.dt_controller)

self.random_track_f = Generate_Random_Trace_Function(

length_of_experiment=self.length_of_experiment,
Expand Down Expand Up @@ -674,7 +721,7 @@ def run_cartpole_random_experiment(self,
else:
raise ValueError('Unknown save mode value')

self.cartpole_ode()
self.cartpole_second_derivatives()

# Create csv file for saving
self.save_history_csv(
Expand Down Expand Up @@ -809,9 +856,9 @@ def set_cartpole_state_at_t0(self, reset_mode=1, s=None, target_position=None, r
pass

# reset global variables
global k, m_cart, m_pole, g, J_fric, M_fric, L, v_max, u_max, controlDisturbance, controlBias, TrackHalfLength, controlDisturbance_mode
global k, m_cart, m_pole, g, J_fric, M_fric, L, v_max, u_max, controlNoiseScale, controlNoiseBias, controlNoiseCorrelation, TrackHalfLength, controlNoise_mode
(k[...], m_cart[...], m_pole[...], g[...], J_fric[...], M_fric[...], L[...], v_max[...], u_max[...],
controlDisturbance[...], controlBias[...], TrackHalfLength[...], controlDisturbance_mode) = CP_PARAMETERS_DEFAULT.export_parameters()
controlNoiseScale[...], controlNoiseBias[...], controlNoiseCorrelation[...], TrackHalfLength[...], controlNoise_mode) = CP_PARAMETERS_DEFAULT.export_parameters()

self.time = 0.0
self.time_last_target_equilibrium_change = None
Expand Down Expand Up @@ -878,13 +925,12 @@ def set_cartpole_state_at_t0(self, reset_mode=1, s=None, target_position=None, r
}
))

self.Q_applied = add_control_noise(self.Q_calculated, rng,
controlDisturbance_mode, controlDisturbance, controlBias)
self.Q_applied = self.control_noise_generator.add_control_noise(self.Q_calculated)

self.Q = self.Q_applied
self.u = self.cpe.Q2u(self.Q) # Calculate CURRENT control input
self.angleDD, self.positionDD = self.cpe.cartpole_ode_interface(self.s, self.u, L=float(L)) # Calculate CURRENT second derivatives

self.u = self.cpe.Q2u(self.Q, u_max) # Calculate CURRENT control input
self.cartpole_second_derivatives() # Calculate CURRENT second derivatives
# Reset the dict keeping the experiment history and save the state for t = 0
self.dt_save_steps_counter = 0
self.dt_controller_steps_counter = 0
Expand Down Expand Up @@ -939,6 +985,7 @@ def dt_controller(self):
@dt_controller.setter
def dt_controller(self, value):
self._dt_controller = value
self.control_noise_generator.reset(dt=self._dt_controller)
if self._dt_simulation is not None:
self.dt_controller_number_of_steps = np.rint(value / self._dt_simulation).astype(np.int32)
if self.dt_controller_number_of_steps == 0:
Expand Down
Loading