AVSLab / basilisk

Astrodynamics simulation framework
https://hanspeterschaub.info/basilisk
ISC License
148 stars 61 forks source link

No support for uint8_t and unsigned char arrays #851

Open wflowersa opened 1 week ago

wflowersa commented 1 week ago

Describe the bug SWIG wrapper does not support arrays of type uint8_t and unsigned char

To reproduce

  1. Make copy of CModuleTemplateMsgPayload called TemplateCharMsgPayload and change dataVector type from double to unsigned char or uint8_t.
  2. Run script posted below (condensed version of example from stand-alone message tutorial)
  3. See error: TypeError: in method 'TemplateCharMsgPayload_dataVector_set', argument 2 of type 'unsigned char [3]'

Expected behavior Script should be able to set dataVector array without error.

System Info

Additional context This appears to be an extension of this issue that was resolved with this pull request; however, support for uint8_t and unsigned char were not added. By adding the following lines to basilisk/src/architecture/_GeneralModuleFiles/swig_conly_data.i in their respective ARRAYASLIST and ARRAY2ASLIST locations, the issue appears to be resolved on my local machine:

ARRAYASLIST(uint8_t, PyLong_FromUnsignedLong, PyLong_AsUnsignedLong) ARRAYASLIST(unsigned char, PyLong_FromUnsignedLong, PyLong_AsUnsignedLong) ARRAY2ASLIST(uint8_t, PyLong_FromUnsignedLong, PyLong_AsUnsignedLong) ARRAY2ASLIST(unsigned char, PyLong_FromUnsignedLong, PyLong_AsUnsignedLong)

Example script:

import sys
import matplotlib.pyplot as plt
from Basilisk.architecture import messaging
from Basilisk.moduleTemplates import cppModuleTemplate
from Basilisk.utilities import SimulationBaseClass
from Basilisk.utilities import macros
from Basilisk.utilities import unitTestSupport

def run():
    #  Create a sim module as an empty container
    scSim = SimulationBaseClass.SimBaseClass()

    #  create the simulation process
    dynProcess = scSim.CreateNewProcess("dynamicsProcess")

    # create the dynamics task and specify the integration update time
    dynProcess.addTask(scSim.CreateNewTask("dynamicsTask", macros.sec2nano(1.)))

    # create modules
    mod1 = cppModuleTemplate.CppModuleTemplate()
    mod1.ModelTag = "cppModule1"
    scSim.AddModelToTask("dynamicsTask", mod1)

    # create stand-alone input message
    msgData = messaging.TemplateCharMsgPayload()
    msgData.dataVector = [1, 2, 3]
    print(msgData.dataVector)
    msg = messaging.TemplateCharMsg().write(msgData)

    msgData.dataVector = [4, 5, 6]
    print(msgData.dataVector)
    msg = messaging.TemplateCharMsg().write(msgData)

if __name__ == "__main__":
    run()
sassy-asjp commented 4 days ago

I think your solution is the correct one for this problem. I recommend you submit a pull request for it.