uniba-swt / libbidib

A library for communication with a BiDiB (www.bidib.org) system using a serial connection.
GNU General Public License v3.0
10 stars 4 forks source link

Locomotive Orientation #2

Closed eyip002 closed 4 years ago

eyip002 commented 5 years ago

In Section 4.7.7 of the BiDiB standard, the address format of locomotives is described. An address has a low and high part, called addrl and addrh respectively.

What is interesting is that bits 7 and 6 of addrh (assuming 0 indexing) provides the locomotive's orientation relative to the track. 00 means that the locomotive faces one way, while 10 means it faces the other.

Could this orientation information be included in the train state? https://github.com/uniba-swt/libbidib/blob/92472ed1faa734e72426929d23b72498eb24d2ce/src/state/bidib_state_intern.h#L49-L58

https://github.com/uniba-swt/libbidib/blob/92472ed1faa734e72426929d23b72498eb24d2ce/include/definitions/bidib_definitions_custom.h#L255-L258

One of the places where locomotive orientation is updated? https://github.com/uniba-swt/libbidib/blob/92472ed1faa734e72426929d23b72498eb24d2ce/src/state/bidib_state_setter.c#L607

https://github.com/uniba-swt/libbidib/blob/dd0b871d06c15154f4bc571853da12288dc21ce7/src/state/bidib_state.c#L610

eyip002 commented 4 years ago

I can confirm, through experimentation on a live system, that this is the way to detect and record the orientation of a train.

Recording the train orientation in bidib_state_cs_drive() is not the right place; this function is only responsible for sending MSG_CS_DRIVE commands. https://github.com/uniba-swt/libbidib/blob/dd0b871d06c15154f4bc571853da12288dc21ce7/src/state/bidib_state_setter.c#L208

eyip002 commented 4 years ago

Commit: d2ed8ca3670a01cbba6de850f4812c9ad03cf4c0

Changes to include/definitions/bidib_definitions_custom.h

Extended struct t_bidib_dcc_address with type to store the two MSB of an address that defines the type of address (locomotive or accessory decoder). For locomotives, type determines whether the train is in the left or right orientation.

Renamed the struct t_bidib_train_direction and its values in terms of orientation. Renamed struct variable t_bidib_train_state_data.direction to t_bidib_train_state_data.orientation.

Extended struct t_bidib_train_position_query with orientation_is_left for train orientation information.

Changes to src/state/bidib_state_intern.h

Extended struct t_bidib_train_state_intern with orientation.

Changes to src/highlevel/bidib_highlevel_getter.c, bidib_config_parser_track.c, bidib_config_parser_train.c

Updated the following functions to support the updated structs:

Changes to src/state/bidib_state.c

Extended function bidib_state_update_train_available() to update the train's current orientation.

Updated function bidib_state_reset() to support the updated struct.

Changes to src/state/bidib_state_getter.c

Implemented a new function bidib_state_get_segment_state() that returns a deep copy of a given segment's state. Needed to log the trains and their current orientation as they enter and exit a segment.

Changes to src/state/bidib_state_setter.c

Removed from function bidib_state_cs_drive() the updating of train orientation , because this is not the right place; orientation is unknown by the function.

Updated function bidib_state_log_train_detect() to log orientation information.

Updated function bidib_state_bm_address_log_changes() to store the orientation of a new train address.

Updated function bidib_state_bm_address() to extract the orientation of all train addresses, and to log train movements after the train states have been updated. To log train movements with proper orientation information, the previous train states have to be saved temporarily while the current list of trains on each segment is updated.