-
Notifications
You must be signed in to change notification settings - Fork 0
CSV to Launch file #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SuragNuthulapaty
wants to merge
6
commits into
main
Choose a base branch
from
csv-launch-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c9188c3
in theory a converter
SuragNuthulapaty a2f4939
initial cpp code
SuragNuthulapaty 4fb4dd6
made it work
SuragNuthulapaty 3facda2
removed darta files
SuragNuthulapaty 719bac5
added comments
SuragNuthulapaty 57367cc
correct yaw
SuragNuthulapaty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import argparse | ||
| import pathlib | ||
| import enum | ||
| import struct | ||
| import csv | ||
|
|
||
| class ReadingDiscriminant(enum.Enum): | ||
| ID_LOWG = 1 | ||
| ID_HIGHG = 2 | ||
| ID_BAROMETER = 3 | ||
| ID_CONTINUITY = 4 | ||
| ID_VOLTAGE = 5 | ||
| ID_GPS = 6 | ||
| ID_MAGNETOMETER = 7 | ||
| ID_ORIENTATION = 8 | ||
| ID_LOWGLSM = 9 | ||
| ID_FSM = 10 | ||
| ID_KALMAN = 11 | ||
| ID_PYRO = 12 | ||
|
|
||
| """ | ||
| This function takes a sensor's discriminant (which is basically an ID) | ||
| and returns all the data that needs to be saved | ||
|
|
||
| That data can take 4 forms: | ||
|
|
||
| string column name: name of a column in the given csv | ||
| None: a field that does not matter/ is not in the csv | ||
| 1: a boolean that needs to be padded to 4 bytes | ||
| 0: a 1 byte value that does not need to be padded | ||
|
|
||
| These cases are very specific to the current csv/ MIDAS configuration, | ||
| but can be changed for different configurations | ||
| """ | ||
| def discriminant_to_field_names(disc): | ||
| match disc: | ||
| case ReadingDiscriminant.ID_LOWG: | ||
| return ["accel_x", "accel_y", "accel_z"] | ||
| case ReadingDiscriminant.ID_HIGHG: | ||
| return ["accel_x", "accel_y", "accel_z"] | ||
| case ReadingDiscriminant.ID_BAROMETER: | ||
| return [None, None, "baro_alt"] | ||
| case ReadingDiscriminant.ID_CONTINUITY: | ||
| return [None, None, None, None, None] | ||
| case ReadingDiscriminant.ID_VOLTAGE: | ||
| return [None] | ||
| case ReadingDiscriminant.ID_GPS: | ||
| return [None, None, None, None, None, None] | ||
| case ReadingDiscriminant.ID_MAGNETOMETER: | ||
| return [None, None, None] | ||
| case ReadingDiscriminant.ID_ORIENTATION: | ||
| return [1, "ang_pos_y", "ang_pos_x", "ang_pos_z", "ang_vel_y", "ang_vel_x", "ang_vel_z", "ang_accel_y", "ang_accel_x", "ang_accel_z", "accel_x", "accel_y", "accel_z", "imu_gyro_x", "imu_gyro_y", "imu_gyro_z", None, None, None, None, None, "alpha"] | ||
| case ReadingDiscriminant.ID_LOWGLSM: | ||
| return ["imu_gyro_x", "imu_gyro_y", "imu_gyro_z", "accel_x", "accel_y", "accel_z"] | ||
| case ReadingDiscriminant.ID_FSM: | ||
| return [None] | ||
| case ReadingDiscriminant.ID_KALMAN: | ||
| return ["kalman_pos_x", "kalman_pos_y", "kalman_pos_z", "kalman_vel_x", "kalman_vel_y", "kalman_vel_z", "kalman_accel_x", "kalman_accel_y", "kalman_accel_z", "baro_alt"] | ||
| case ReadingDiscriminant.ID_PYRO: | ||
| return [0, 0, 0, 0, 0, 0, 0, 0, 0] | ||
|
|
||
| """ | ||
| Takes a csv line and returns a list of each of the sensor packets | ||
| that will be written as bytes | ||
| """ | ||
| def line_to_bytes(line): | ||
| all_packets = [] | ||
| for disc in ReadingDiscriminant: | ||
| current_packet = [] | ||
| cols_to_save = discriminant_to_field_names(disc) | ||
|
|
||
| # Write the discriminant | ||
| current_packet.append(disc.value.to_bytes(4, 'little')) | ||
|
|
||
| # Write the timestamp as an integer | ||
| current_packet.append(int(float(line['time']) * 1000).to_bytes(4, 'little')) | ||
|
|
||
| for col in cols_to_save: | ||
| # Writes a 4 byte float | ||
| if col is None: | ||
| current_packet.append(struct.pack('f', 0)) | ||
| # Writes a boolean 1 with 4 bytes padding | ||
| elif col == 1: | ||
| current_packet.append(col.to_bytes(4, "little")) | ||
| # Writes a boolean 0 with no padding | ||
| elif col == 0: | ||
| current_packet.append(col.to_bytes(1, "little")) | ||
| # Writes the value of the column as a float | ||
| else: | ||
| if col == "ang_pos_y": | ||
| current_packet.append(struct.pack('f', -float(line[col]))) | ||
| else: | ||
| current_packet.append(struct.pack('f', float(line[col]))) | ||
|
|
||
| all_packets.append(current_packet) | ||
|
|
||
| return all_packets | ||
|
|
||
|
|
||
|
|
||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("infile", type=pathlib.Path) | ||
| parser.add_argument("outfile", type=pathlib.Path) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| in_file = args.infile | ||
| out_file = args.outfile | ||
|
|
||
| all_rows = [] | ||
|
|
||
| with open(in_file, 'r') as f: | ||
| csv_reader = csv.DictReader(f) | ||
| for row in csv_reader: | ||
| all_rows.append(line_to_bytes(row)) | ||
|
|
||
|
|
||
|
|
||
| num = 0xf3626b6a | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the checksum, add a note or rename the variable to specify what that is |
||
| buf = 0x00 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this buf for? |
||
|
|
||
| with open(out_file, 'wb') as f: | ||
| f.write(num.to_bytes(4, 'little')) | ||
| for row in all_rows: | ||
| for packet in row: | ||
| for data in packet: | ||
| f.write(data) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment to say that you noticed that most of the values with this parameter is a 4 byte float