Sample (with comments) autonomous program.
2571 14 2019-9-5
Uploading and Loding Picture ...(0/1)
o(^-^)o
BGA
Second Officer
Offline

Here is something I come up with to get the Robomaster S1 to automatically return fire when hit while at the same time allowing it to be controlled by the user. Copying and pasting this code to the DYI programming lab should work:

# Turn to the side a hit came from. It should be called when a hit is detected.
def user_defined_turn_to_hit_side():
    if (armor_ctrl.check_condition(rm_define.cond_armor_bottom_right_hit)) or (armor_ctrl.check_condition(rm_define.cond_armor_top_right_hit)):
        # Hit on the right side. Turn right.
        gimbal_ctrl.rotate_with_degree(rm_define.gimbal_right,90)
    else if (armor_ctrl.check_condition(rm_define.cond_armor_bottom_left_hit)) or (armor_ctrl.check_condition(rm_define.cond_armor_top_left_hit)):
        # Hit on the left side. Turn left.
        gimbal_ctrl.rotate_with_degree(rm_define.gimbal_left,90)
    else if armor_ctrl.check_condition(rm_define.cond_armor_bottom_back_hit):
        # Hit on the back side. Turn back.
        gimbal_ctrl.rotate_with_degree(rm_define.gimbal_right,180)

# Program entry point. This is the start of our code and is implicitly called by the actual main function.
def start():
    # Gimbal lead mode. Chassis follow the direction of the gimbal.
    robot_ctrl.set_mode(rm_define.robot_mode_chassis_follow)

    # Set rotation speed to 360 degrees per second.
    gimbal_ctrl.set_rotate_speed(360)

    # Allows controlling the gimbal with the controller in autonomous mode.   
    gimbal_ctrl.enable_stick_overlay()

    # Allows controlling the chassis with the controller in autonomous mode.   
    chassis_ctrl.enable_stick_overlay()

    # This keeps the autonomous program running otherwise it would exit. We sleep here so not to hog the CPU.
    # 3600 seconds is an arbitrarily large time to sleep but even a second should be ok. Our main code runs on a
    # callback anyway so the large sleep time does not matter.
    while True:
        time.sleep(3600)

# This is an API provided callback and called automatically whenever a hit is detected.
def armor_hit_detection_all(msg):
    # Turn to the side we got hit from.
    user_defined_turn_to_hit_side()

    # TODO: Use S1 detection to find and center on the attacking robomaster before firing.
  
    # Fire once. Note this does not appear to really work in autonomous mode. Looks like a bug to me.
    gun_ctrl.fire_once()

    # TODO: Move back to the original position (easy, just need to keep track of the direction we turned. Might not
    #              be desirable as we might be contantly hit from that side.


2019-9-5
Use props
rhoude57 - YUL
lvl.4
Offline

It seems that gun_ctrl.fire_once() has to be preceded by
gun_ctrl.set_fire_count(1)
2019-9-5
Use props
rhoude57 - YUL
lvl.4
Offline

Nice! I am working on a spin-off that will include the auto aim and fire code...
2019-9-5
Use props
BGA
Second Officer
Offline

rhoude57 - YUL Posted at 9-5 14:58
It seems that gun_ctrl.fire_once() has to be preceded by
gun_ctrl.set_fire_count(1)

Possibly, but I am pretty sure I saw at least one example where fire_once() was called without setting the fire rate. From this weekend on I will be able to play more with my S1s so I will investigate this (and other things).
2019-9-5
Use props
BGA
Second Officer
Offline

BGA Posted at 9-5 15:16
Possibly, but I am pretty sure I saw at least one example where fire_once() was called without setting the fire rate. From this weekend on I will be able to play more with my S1s so I will investigate this (and other things).

The programming guide says this about fire_once():

Note:
Single Shot mode means one bead is fired each time by default.
You can shoot several beads at once by setting the “set blaster to fire (1) bead/time” block.

So it seems to imply that you only need to call set_fire_count() if you want more than one bead to be fired at a time.

Ps: I really miss not being able to fire the IR blaster.
2019-9-5
Use props
MarkusXL
lvl.4
Offline

I'm getting a syntax error on line 5, the first long if statement.  I could be because I am copying hidden "\r" line feed characters after copy / paste.

I encourage all us coders to set up, very simply and free, a GitHub repository.  Then we can even contribute to each other's code, spawn Branches, Merge in stuff we like from other programs, etc.

It's what "real coders" all do!  
2019-9-5
Use props
BGA
Second Officer
Offline

MarkusXL Posted at 9-5 20:15
I'm getting a syntax error on line 5, the first long if statement.  I could be because I am copying hidden "\r" line feed characters after copy / paste.

I encourage all us coders to set up, very simply and free, a GitHub repository.  Then we can even contribute to each other's code, spawn Branches, Merge in stuff we like from other programs, etc.

I can not see anything obviously wrong on that line but I do not have the S1 here to test (I am at work). Just to be sure, check your indentation as indentation is syntax in Python (this is one of the reasons why i do not like it that much).

2019-9-6
Use props
BGA
Second Officer
Offline

BGA Posted at 9-6 07:23
I can not see anything obviously wrong on that line but I do not have the S1 here to test (I am at work). Just to be sure, check your indentation as indentation is syntax in Python (this is one of the reasons why i do not like it that much).

This actually reminds me that a first step to simplify development might be to simply implement stubbed out versions of all the Robomaster APIs. You will not be able to run programs outside of the S1 but, at least, syntax errors can be caught and fixed without having to use the terrible interface in the S1 app.  
2019-9-6
Use props
rhoude57 - YUL
lvl.4
Offline

MarkusXL Posted at 9-5 20:15
I'm getting a syntax error on line 5, the first long if statement.  I could be because I am copying hidden "\r" line feed characters after copy / paste.

I encourage all us coders to set up, very simply and free, a GitHub repository.  Then we can even contribute to each other's code, spawn Branches, Merge in stuff we like from other programs, etc.

The syntax error, in my case anyway, was caused by a missing closing parenthesis towards the end of the code.
2019-9-6
Use props
DJI Stephen
DJI team
Offline

Hello and good day BGA. Thank you for sharing this information and this program you have created. Thank you for your support.
2019-9-6
Use props
BGA
Second Officer
Offline

MarkusXL Posted at 9-5 20:15
I'm getting a syntax error on line 5, the first long if statement.  I could be because I am copying hidden "\r" line feed characters after copy / paste.

I encourage all us coders to set up, very simply and free, a GitHub repository.  Then we can even contribute to each other's code, spawn Branches, Merge in stuff we like from other programs, etc.

Sure, but the fact that the code can not be ran outside of the S1 does complicate things a bit. I will look into creating a stub to at least allow some form of development to happen (as I mentioned in a different reply). In fact, doing this now might allow to expand it later to do more than just stubs and actually test behavior (even if in a limited way). We might create our own text-only Robomaster S1 emulator.

2019-9-6
Use props
BGA
Second Officer
Offline

rhoude57 - YUL Posted at 9-6 07:52
The syntax error, in my case anyway, was caused by a missing closing parenthesis towards the end of the code.

But that was a copy and paste error, right? I can not see a missing parenthesis anywhere (I might just be missing it though).
2019-9-6
Use props
BGA
Second Officer
Offline

Oh, one important thing I forgot to mention. To actually get this to work as expected, you see to use the DIY Lab to set this as an autonomous program (touch the 3 dots in this program when seeing the program list and mark it as autonomous). This will upload the program to the Robomaster S1.

Then, to actually run it, you need to press the autonomous program button on the side of the intelligent controller. To test if it is working, use something to hit any of the hit detectors (you might need to apply some force to the hit) and the S1 should turn to the side it was hit (but, unfortunately, not fire due to what looks like a bug).
2019-9-6
Use props
BGA
Second Officer
Offline

DJI Stephen Posted at 9-6 08:12
Hello and good day BGA. Thank you for sharing this information and this program you have created. Thank you for your support.

Glad to help. Can you ask the developers to prioritize enabling the blaster fire in autonomous mode (even better if they can add the IR blaster APIs that are missing)? This should not be that difficult I guess, it is just a matter of flipping some logical switch somewhere.
2019-9-6
Use props
DJI Stephen
DJI team
Offline

BGA Posted at 9-6 08:28
Glad to help. Can you ask the developers to prioritize enabling the blaster fire in autonomous mode (even better if they can add the IR blaster APIs that are missing)? This should not be that difficult I guess, it is just a matter of flipping some logical switch somewhere.

You are very much welcome BGA. Rest assured that this information will be cascaded to the designated DJI department for further development of the DJI Robomaster S1. If you have any other request or concerns with DJI or with the DJI Robomaster S1. Please feel free to post it here at DJI Forum. We are here to help and support you. Thank you for your valued support.
2019-9-6
Use props
Advanced
You need to log in before you can reply Login | Register now

Credit Rules