Shop OBEX P1 Docs P2 Docs Learn Events
XML Script help — Parallax Forums

XML Script help

chaosgkchaosgk Posts: 322
edited 2010-06-09 21:54 in General Discussion
I am looking for a volunteer that could write a simple script for me if anyone is interested.· This is for my fireworks firing system Kwinn and I are building using a prop as the controller.

I need a file output that looks like this;

long· s2 + RY56
long· RY1
long· at +· 2000


From an input that looks like this.

long s1 +· RY49 long at 11400
long s1 +· RY1 long at 19600
long s1 +· RY2 long at 20600

The simulation program I use exports it into a CSV file. I tried using excel to create what I want, but there needs to be lines seperating the commands.

Here is how it works from the first line.
long s1 +· RY49 long at 11400

1st line =· slave select (s1 for example) + Relay number (ry49) + Next relay number if there is one at the same time code,·· Can only be RY 33-49, otherwise blank after the slave #
2nd line =· slave select 1-32,· if not 1-32, then 0
3rd line = 11400· (time in ms)· If there are other slaves and relays selected at the same time, it should·wait to add the time code line until after all the slaves and relays have been selected.

example;·
input:
long s1 +· RY49 long at 11400
long s1 +· RY1 long at 20600
long s1 +· RY32 long at 20600
long s2 +· RY2 long at 20600

output
long·s1 + ry49
long 0
long at + 11400
long s1
long ry1 + RY32
long s2
long ry2
long at + 20600

If there is more than one relay selected on the same slave in the group 33-56 or 1-32, they can be entered as;

long s1 + ry33 + ry49
long RY1 + RY32
long at +2000

Also, the data it pulls from can be in a CSV format of;
11.4,1,49· (time in seconds, slave, Relay)


If you can help, please, please let me know.· I can do it manually, but it would take me days to go through and edit it, and if I have to change the time codes or addresses in the simulation software, it would be a nightmare to try and adjust it in the code.

Thanks
Derrick


·




·

Comments

  • mctriviamctrivia Posts: 3,772
    edited 2010-05-28 02:36
    this has nothing to do with xml.

    however it should not be to hard to write a php script to make the conversion

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Lots of propeller based products in stock at affordable prices.
  • chaosgkchaosgk Posts: 322
    edited 2010-05-28 03:02
    Someone told me I'd need an xml script for it, but i'm not picky how it gets written, I just have no idea how to do it.
  • mctriviamctrivia Posts: 3,772
    edited 2010-05-28 03:10
    i am not entirely sure what steps are used to convert the input to the output but if you can write exactly what steps are needed to convert the 2 i can write the script to do it. provide example files.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Lots of propeller based products in stock at affordable prices.
  • chaosgkchaosgk Posts: 322
    edited 2010-05-28 03:56
    Ok, I'm doing a simulation right now, once it's done, I'll export it and post it here
  • rosco_pcrosco_pc Posts: 468
    edited 2010-05-28 05:20
    Been lurking long enough, time to contribute something. If you don't mind python you can do this:

    import sys, csv
    
    # Simple python script to change cvs input to single line output
    # save it somewhere as convert.py
    # Open a command window and type "python convert.py INPUT OUTPUT"
    #   INPUT - csv file
    #   OUTPUT - your wanted output
    
    reader = csv.reader(open(sys.argv))
    writer = open(sys.argv,'w')
    for row in reader:
        # Determine relay groups using list comprehension
        # this assumes that relays are just added as extra values in the csv file
        # A csv line of:
        # time,   slave,  relay1, relays, ..., relayN
        # produces a python list of:
        # row[noparse][[/noparse]0], row, row, row, ...
        #
        # list can be 'sliced', e.g row[noparse][[/noparse]2:] indcates all values after index 2
    
        relay_group1 = [noparse][[/noparse]ry for ry in row[noparse][[/noparse]2:] if (int(ry) > 0 and int(ry) < 33)]
        relay_group2 = [noparse][[/noparse]ry for ry in row[noparse][[/noparse]2:] if (int(ry) > 32 and int(ry) < 50)]
    
        # write first line slave + relay in group 2
        writer.write('long s%s'%row)
        for r in relay_group2:
            writer.write('+ry%s'%r)
        writer.write('\n')
    
        # write second line, relays in group 1 or '0'
        # first we construct the relay string to write
        out = ''
        for r in relay_group1:
            out += '+ry%s'%r
    
        # Check if there are any relays to write
        if out:
            writer.write('long %s\n'%out[noparse][[/noparse]1:])  # Skip the first '+'
        else:
            writer.write('long 0\n')
    
        # write time
        time = int(float(row[noparse][[/noparse]0])*1000)
        writer.write('long at +%s\n'%time) 
    
    
    



    You were not very clear on how the relays were added to the CSV file, so I guessed a bit here. Something very similar can be easily be done in VBA (excel)

    [noparse][[/noparse]edit]
    If the assumption was not correct about how you added relays for a slave to the csv file let me and I will change the code.

    I also converted this to an excel macro (being a bit bored at work smilewinkgrin.gif) I attached the result (most of the work is a macro, so you might be asked to enable macro's depending on your security settings). Just press the big CONVERT button and select an csv file. Result will be a txt file written in the same folder as the csv file. If you don't trust to open an unknown excel file here is the code:

    
    Sub convert()
        ' This will convert a csv file 'name.csv' in a text file 'name.txt'
        
        
        ' Select a CSV file
        With Application.FileDialog(msoFileDialogFilePicker)
            .Title = "Select CSV file"
            .InitialFileName = ActiveWorkbook.Path
            .Filters.Add "CSV file", "*.csv", 1
            If .Show = -1 Then
                mypath = .SelectedItems(1)
            Else                                            ' Pressed Cancel =>
                Exit Sub                                    ' leave Macro
            End If
        End With
        
        Workbooks.Open (mypath)                             ' Open selected file
        
        Set iWS = ActiveWorkbook.Worksheets(1)              ' Make sure to use the correct worksheet
        ActiveWorkbook.Worksheets.Add after:=iWS            ' Create a new sheet to store the converted data
        Set nWS = ActiveWorkbook.Worksheets(2)
        For iRow = 1 To iWS.UsedRange.Rows.Count
            line1 = "long s" & iWS.Cells(iRow, 2).Value     ' Slave + relays 33-49
            line2 = ""                                      ' relays 1-32
            line3 = iWS.Cells(iRow, 1).Value * 1000         ' time
            For iColumn = 3 To iWS.UsedRange.Columns.Count
                c = iWS.Cells(iRow, iColumn).Value          ' empty cell is indicated with 0, so check for that
                If c > 0 And c < 33 Then                    ' Relays 1-32 belong in line2
                    line2 = line2 & "+ry" & c
                ElseIf c > 32 And c < 50 Then               ' Relays 33-49 in line1
                    line1 = line1 & "+ry" & c
                End If
            Next iColumn
            If line2 = "" Then
                line2 = "long 0"                            ' No relays in group2
            Else
                line2 = "long " & Mid(line2, 2)             ' Remove first '+'
            End If
            nWS.Cells((iRow - 1) * 3 + 1, 1).Value = line1
            nWS.Cells((iRow - 1) * 3 + 2, 1).Value = line2
            nWS.Cells((iRow - 1) * 3 + 3, 1).Value = "long at +" & line3
        Next iRow
        
        Application.DisplayAlerts = False                   ' Turn off the confirmation dialogue
        iWS.Delete                                          ' remove csv data
        Application.DisplayAlerts = True                    ' And on again
    
        nFile = Left(mypath, InStrRev(mypath, ".")) & "txt"
        ActiveWorkbook.SaveAs Filename:=nFile, FileFormat:=-4158     ' save as text file
        ActiveWorkbook.Close SaveChanges:=False
    End Sub
    
    



    Just copy this in an excel spreadsheet and add a button so you can start the converter

    Post Edited (rosco_pc) : 5/28/2010 8:48:41 AM GMT
  • chaosgkchaosgk Posts: 322
    edited 2010-06-03 18:52
    Thanks Rosco, I have been very busy lately and haven't checked the thread. I'll test this when I get home tonight and let you know how it works.
    Thanks a lot.

    ·
  • rosco_pcrosco_pc Posts: 468
    edited 2010-06-04 04:15
    You're welcome.

    BTW saw now that the forum mangles the python code (losing array indexes), so I've attached the code here with a small bonus smile.gif
  • chaosgkchaosgk Posts: 322
    edited 2010-06-08 05:41
    The excel macro worked pretty well If you can, I need a couple of simple changes made.
    the + need a space in front and behind them so instead of "long at+213200" it would be "long at + 213200"
    Same with the RY commands.
    Also, if a time code is the same for two of the S's (ie. S1, S2, ...) the time code should go at the end of the set such as.

    long s1
    Long RY1
    long s2
    long ry11
    long at 213200

    That way the firing program reads all of the things that should happen at that time and sets them to go off together, otherwise it sees the 2nd time code at the same time and only fires one set.

    Hope this makes sense and is a quick fix.
    Other then those two things, it works awesome.
    Thanks
    Derrick.

    BTW, if you want to check out some of our videos of the testing of the fireworks, go to http://www.youtube.com/user/mumpyrotechnics
    or check mumpyro.com
  • rosco_pcrosco_pc Posts: 468
    edited 2010-06-08 11:46
    The 1st change was easy. The 2nd turned out to be easy as well, assuming that time are sequentially stored in the CSV file. This means that you will have
    11.4, 1, 1,33,4,...
    11.4, 2, 20,25,49
    12.1, ....

    Let me know if this is not the case and I will add some sorting. The attached zip file contains both Excel file and the 'raw' vba file.

    Unfortunately I do not have access to youtube as I'm sitting behind the Great Firewall of China and it seems they have finally found a way to block tor sad.gifcry.gif
  • chaosgkchaosgk Posts: 322
    edited 2010-06-08 22:44
    yes, the times are stored sequentially in the csv. I will give the new one a try tonight and see how it runs.
    Thanks
  • chaosgkchaosgk Posts: 322
    edited 2010-06-09 05:13
    Ok, found one minor bug with the excel file. If the RY is over 50, it sets it as a long 0, instead of the RY#. Other then that, it looks good.
  • rosco_pcrosco_pc Posts: 468
    edited 2010-06-09 12:11
    Well this is what you specified in the first post
    chaosgk said...

    1st line = slave select (s1 for example) + Relay number (ry49) + Next relay number if there is one at the same time code, Can only be RY 33-49, otherwise blank after the slave #
    2nd line = slave select 1-32, if not 1-32, then 0

    I understood this to mean that 1st line contains slave + relays 33-49 and 2nd line contains relays 1-32 or just a '0' if no relays between 1-32 have been selected. But I now also see that you said:
    chaosgk said...

    If there is more than one relay selected on the same slave in the group 33-56 or 1-32, they can be entered as;
    This is something you can quite easily change yourself (you need to learn that anyway, as I might not always be around wink.gif ):
    • Open the visual basic editor via the excel menu Tools > Macro > Visual Basic Editor
    • Double click on ThisWorkbook in the VBAProject list (left hand side). Make sure that this is in the right excel sheet: VBAProject(convert.xls), but this should have been selected automatically when you opened the VBA editor
    • change the line:
         ElseIf c > 32 and c < 50 Then           ' relays 33-49 in line1
      
      


      to
        ElseIf c > 32 and c < 57 Then           ' relays 33-56 in line1
      
      

    • go back to excel and save file
    • test if it works as expected

    edited to correct some spelling mistakes

    Post Edited (rosco_pc) : 6/9/2010 12:23:10 PM GMT
  • chaosgkchaosgk Posts: 322
    edited 2010-06-09 14:58
    Sounds good, I'll change it tonight when I get home.
    Thanks
  • rosco_pcrosco_pc Posts: 468
    edited 2010-06-09 21:54
    I did not have my work laptop with me otherwise I would have made the change tongue.gif

    All my machines at home have different flavors of Linux and/or OpenBSD installed
Sign In or Register to comment.