Servo motor MG995 keeps rotating when program has ended - raspberry-pi

I bought the MG995 55g 360° servomotor and I am trying to control it with my Raspberry Pi using the following code:
from gpiozero import Servo
from time import sleep
from gpiozero.pins.pigpio import PiGPIOFactory
factory = PiGPIOFactory()
myGPIO = 12
servo = Servo(myGPIO, min_pulse_width=0.9/1000, max_pulse_width=2.1/1000, pin_factory=factory)
try:
print("start in the middle")
servo.mid()
sleep(5)
print("go to min")
servo.min()
sleep(5)
print("go to max")
servo.max()
sleep(5)
print("and back to the middle")
servo.mid()
sleep(5)
except KeyboardInterrupt:
print("keyboard interrupt")
except:
print("some error")
finally:
print("finishing...")
servo.value = None
When the program is finished, my servo starts continuously rotating or moving to random positions at random times. I can only stop it by unplugging it. What is going wrong?

Related

How to make LED blink at second press on push button?

I am very new to stm32, I have STM32F411RE Nucleo and I work in STM32CubeIDE. First I need to turn on the LED with the first click on the push button, and the LED stays on until the next click. On the second click, the LED starts blinking with a frequency of 10 Hz, then I need a third click, a fourth, and so on. I have written the code for the first one and it works but I don't know how to make the LED do something with every click on the push button. I don't know how to write the program so it recognizes when a button is pressed for the second, third, or fourth time...
So far I have this:
// read the status of the GPIO button pin
button_val = HAL_GPIO_ReadPin(GPIOC, B1_Pin);
// check if it is high or low
if(button_val == PRESSED) // the button is pressed
{
// if pressed - turn led on
HAL_GPIO_WritePin(LD2_GPIO_Port,LD2_Pin, SET);
// if not pressed - turn led off
// HAL_GPIO_WritePin(LD2_GPIO_Port,LD2_Pin, RESET);
}
When I run this the LED lights up when I press the button. Then I have this code for the second press on the button but it doesn't work.
if(button_val == SECONDPRESSED)
{
// turn led on
HAL_GPIO_WritePin(LD2_GPIO_Port,LD2_Pin, SET);
HAL_Delay(1000);
// turn led off
HAL_GPIO_WritePin(LD2_GPIO_Port,LD2_Pin, RESET);
HAL_Delay(1000);
}
This is a general programming question and not really a STM32-specific issue, since the necessary STM32 hardware input/output seems to work already in your case.
In the STM32F4xx HAL driver code it can be seen that HAL_GPIO_ReadPin() returns a value of type GPIO_PinState, which can be in two states, GPIO_PIN_SET or GPIO_PIN_RESET, GPIO pins are either in HIGH or LOW state.
Now, to make the program cycle through different program states becomes a matter of software programming, there are no hardware specific functions involved. To realize the desired behavior, a very basic state machine could be used for example, using the following states:
typedef enum {
PROG_STATE_INIT = 0,
PROG_STATE_FIRST_PRESS,
PROG_STATE_SECOND_PRESS,
PROG_STATE_THIRD_PRESS,
PROG_STATE_FOURTH_PRESS,
_PROG_STATE_COUNT
} prog_state;
Then, in the main loop, the program could cycle through the states for example like this:
static prog_state _prog_state = PROG_STATE_INIT;
static GPIO_PinState _pin_state_prev = GPIO_PIN_RESET;
const GPIO_PinState pin_state = HAL_GPIO_ReadPin(GPIOC, B1_Pin);
if (pin_state == GPIO_PIN_SET && pin_state != _pin_state_prev) {
++_prog_state; /* next state */
if (_prog_state >= _PROG_STATE_COUNT) {
_prog_state = PROG_STATE_FIRST_PRESS;
}
}
_pin_state_prev = pin_state;
switch (_prog_state) {
case PROG_STATE_INIT:
/* do nothing here, LED is already off */
break;
case PROG_STATE_FIRST_PRESS:
/* turn LED on and let it stay on */
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
break;
case PROG_STATE_SECOND_PRESS:
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
HAL_Delay(50); /* ~10 Hz blinky */
break;
case PROG_STATE_THIRD_PRESS:
/* TODO */
break;
case PROG_STATE_FOURTH_PRESS:
/* TODO */
break;
case _PROG_STATE_COUNT:
default:
/* unexpected */
break;
}
Like already mentioned in the comments above, software/hardware button debouncing might be needed. But for this exercise, it can also be OK to simply add a short delay to the main loop, so that fast GPIO state changes have no effect.
And to expand on the exercise, GPIO EXTI interrupts could be used to react to GPIO state changes. And it might be interesting to set up a timer/counter to let the LED blink precisely at 10 Hz, and control it via the state machine.

Does anyone know how i center my mouse in the middle with python?

The problem I currently have is that the mouse pointer is in the middle bottom of my screen and I am not able to move it whatsoever. I need the mouse pointer in the middle of my screen so I can use it for cookie clicker.
Edit: i am making this auto clicker with my raspberry pi pico.
this is my code for an auto clicker for the Samsung Galaxy A70.
import time
import usb_hid
from adafruit_hid.mouse import Mouse
import board
import digitalio
MIDDLE_BUTTON = 4
RIGHT_BUTTON = 2
LEFT_BUTTON = 1
mouse = Mouse(usb_hid.devices)
led = digitalio.DigitalInOut(board.GP25)
led.direction = digitalio.Direction.OUTPUT
led.value = False
time.sleep(25)
mouse.move(y=360)
while True:
led.value = True
mouse.click(mouse.LEFT_BUTTON)
time.sleep(0.01)
Does anyone know how I can fix this?

How can I change LED's brightness with PWM and USART in USART1_IRQHandler?

I am using STM32F4 discovery board and I am working on changing LED brightness and timer period using ADC and USART.
I use DMA to fetch ADC values, selected TIM4 for PWM and selected USART1 for USART. ADC, USART and PWM are working well so far, but USART1_IRQHandler() is not working as expected.
How can I adjust timer period and LED brightness with PWM and ADC in the following implementation of USART1_IRQHandler()?
void USART1_IRQHandler()
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) )
{
delay(168000);
i = USART_ReceiveData(USART1);
if(i == '1'){
TIMOC_InitStruct.TIM_Pulse=pwm_value;
TIM_OC1Init(TIM4,&TIMOC_InitStruct);
TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);
UARTSend("3 led aktif\r\n",sizeof("LED 4 ON\r\n"));
}
else if(i == '2')
{
TIMOC_InitStruct.TIM_Pulse=pwm_value;
TIM_OC1Init(TIM4,&TIMOC_InitStruct);
TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIMOC_InitStruct.TIM_Pulse=pwm_value;
TIM_OC2Init(TIM4,&TIMOC_InitStruct);
TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Enable);
UARTSend("2 led aktif\r\n",sizeof("LED 4 ON\r\n"));
}
else if(i == '4')
{
TIMOC_InitStruct.TIM_Pulse=pwm_value;
TIM_OC1Init(TIM4,&TIMOC_InitStruct);
TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIMOC_InitStruct.TIM_Pulse=pwm_value;
TIM_OC2Init(TIM4,&TIMOC_InitStruct);
TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIMOC_InitStruct.TIM_Pulse=pwm_value;
TIM_OC3Init(TIM4,&TIMOC_InitStruct);
TIM_OC3PreloadConfig(TIM4,TIM_OCPreload_Enable);
UARTSend("4 led aktif\r\n",sizeof("LED 4 ON\r\n"));
}
else if(i == '4')
{
TIMOC_InitStruct.TIM_Pulse=pwm_value;
TIM_OC1Init(TIM4,&TIMOC_InitStruct);
TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIMOC_InitStruct.TIM_Pulse=pwm_value;
TIM_OC2Init(TIM4,&TIMOC_InitStruct);
TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIMOC_InitStruct.TIM_Pulse=pwm_value;
TIM_OC3Init(TIM4,&TIMOC_InitStruct);
TIM_OC3PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIMOC_InitStruct.TIM_Pulse=pwm_value;
TIM_OC4Init(TIM4,&TIMOC_InitStruct);
TIM_OC4PreloadConfig(TIM4,TIM_OCPreload_Enable);
UARTSend("4 led aktif\r\n",sizeof("LED 4 ON\r\n"));
}
else
{
Hello();
UARTSend("Please enter another number:\n",sizeof("Please enter another number:\n"));
}
} USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
About your USART1_IRQHandler, have you check if the interrupt is active with
__HAL_RCC_UART4_CLK_ENABLE();
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
and if you enable it with this call
UART_Receive_IT( &huart1 );
I am using STM32Cube, repository V1.25.0.
Now, about your code inside the USART1_IRQHandler(), you could use shortcut to handle what you want to do. If I understand, you have 4 leds to control with the Timer4 and 4 channels.
The pulse is the Output Compare value (TIM4->CCR1 for channel 1). So, I would start the timer with a pulse equal to the Period (or zero. See note below).
From that point, if you want to change the brightness, you have just to change the pulse width with
__HAL_TIM_SET_COMPARE( htim4, TIM_CHANNEL_1, pwm_value);
or with
TIM4->CCR1 = pwm_value;
You could also disable any channel with
__HAL_TIM_DISABLE_IT( htim4, TIM_CHANNEL_1);
__HAL_TIM_ENABLE_IT( htim4, TIM_CHANNEL_1);
So, you get something like that
if(i == '1'){
TIM4->CCR1 = pwm_value;
} else if(i == '2') {
TIM4->CCR1 = pwm_value;
TIM4->CCR2 = pwm_value;
} ...
Note: I don't know how are connected your leds. If the led is connected to Vcc and the other end to the MPU pin, a LOW pulse will turn ON the led and HIGH pulse will turn it OFF.

Why Arduino needs to be restarted after PS2 controller communication in Arduino? And how to to do this via coding?

When I ran the code for a drivetrain and opened serial monitor it was working, but when I powered on Arduino via dc Jack and ran the code. It was not working, because code is needed to restart after connection. I want to know that how can I solve this problem of PS2 communication and run my drivetrain without restart or starting serial monitor.
Code that I uploaded was given in this instructable
http://www.instructables.com/id/Control-anything-with-ps2-controller-and-Arduino-/
I got my answer by experimenting a lot. First thing is that you have to either restart your arduino or configure your pin after connection of ps2 to arduino. This can be done by 2 ways :
1) Restart arduino after connection by code or
2) configure again after connection
1 option you know well, i am discussing 2nd i.e configure :
as you know that you have already configured pins by function ps2x.config_gamepad();
Now you have to reconfigure it after connection of ps2 to arduino, you can use a function called reconfig_gamepad(); after read_gamepad(); in void loop.
my code for holonomic drive train is below find out comments // there are total four commented lines in code. good luck. Hope this will solve your problem also.
#include <PS2X_lib.h> //for v1.6
int dir1=13;
int dir2=11;
int dir3=9;
int dir4=7;
int pwm1=12;
int pwm2=10;
int pwm3=8;
int pwm4=6;
int value_mapped;
/******************************************************************
* set pins connected to PS2 controller:
* - 1e column: original
* - 2e colmun: Stef?
* replace pin numbers by the ones you use
******************************************************************/
#define PS2_DAT 44 //14
#define PS2_CMD 46 //15
#define PS2_SEL 48 //16
#define PS2_CLK 50 //17
/******************************************************************
* select modes of PS2 controller:
* - pressures = analog reading of push-butttons
* - rumble = motor rumbling
* uncomment 1 of the lines for each mode selection
******************************************************************/
//#define pressures true
#define pressures false
//#define rumble true
#define rumble false
PS2X ps2x; // create PS2 Controller Class
//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you connect the controller,
//or call config_gamepad(pins) again after connecting the controller.
int error = 0;
byte type = 0;
byte vibrate = 0;
void setup(){
Serial.begin(57600);
pinMode(dir1,OUTPUT);
pinMode(dir2,OUTPUT);
pinMode(dir3,OUTPUT);
pinMode(dir4,OUTPUT);
pinMode(pwm1,OUTPUT);
pinMode(pwm2,OUTPUT);
pinMode(pwm3,OUTPUT);
pinMode(pwm4,OUTPUT);
delay(1000); //added delay to give wireless ps2 module some time to startup, before configuring it
//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
if_error_is_found: // <!---- changed here --->
//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
if(error == 0){
Serial.print("Found Controller, configured successful ");
Serial.print("pressures = ");
if (pressures)
Serial.println("true ");
else
Serial.println("false");
Serial.print("rumble = ");
if (rumble)
Serial.println("true)");
else
Serial.println("false");
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
Serial.println("holding L1 or R1 will print out the analog stick values.");
Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
}
else if(error == 1)
{
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
goto if_error_is_found; // <!---- changed here --->
}
else if(error == 2)
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
else if(error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
// Serial.print(ps2x.Analog(1), HEX);
type = ps2x.readType();
switch(type) {
case 0:
Serial.print("Unknown Controller type found ");
break;
case 1:
Serial.print("DualShock Controller found ");
break;
case 2:
Serial.print("GuitarHero Controller found ");
break;
case 3:
Serial.print("Wireless Sony DualShock Controller found ");
break;
}
}
void loop() {
/* You must Read Gamepad to get new values and set vibration values
ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
if you don't enable the rumble, use ps2x.read_gamepad(); with no values
You should call this at least once a second
*/
if(error == 1) //skip loop if no controller found
return;
if(type == 2){ //Guitar Hero Controller
ps2x.read_gamepad(); //read controller
if(ps2x.ButtonPressed(GREEN_FRET))
Serial.println("Green Fret Pressed");
if(ps2x.ButtonPressed(RED_FRET))
Serial.println("Red Fret Pressed");
if(ps2x.ButtonPressed(YELLOW_FRET))
Serial.println("Yellow Fret Pressed");
if(ps2x.ButtonPressed(BLUE_FRET))
Serial.println("Blue Fret Pressed");
if(ps2x.ButtonPressed(ORANGE_FRET))
Serial.println("Orange Fret Pressed");
if(ps2x.ButtonPressed(STAR_POWER))
Serial.println("Star Power Command");
if(ps2x.Button(UP_STRUM)) //will be TRUE as long as button is pressed
Serial.println("Up Strum");
if(ps2x.Button(DOWN_STRUM))
Serial.println("DOWN Strum");
if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
Serial.println("Start is being held");
if(ps2x.Button(PSB_SELECT))
Serial.println("Select is being held");
if(ps2x.Button(ORANGE_FRET)) { // print stick value IF TRUE
Serial.print("Wammy Bar Position:");
Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
}
}
else { //DualShock Controller
ps2x.read_gamepad(); //read controller and set large motor to spin at 'vibrate' speed
ps2x.reconfig_gamepad(); // <!---- changed here --->
if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
Serial.println("Start is being held");
if(ps2x.Button(PSB_SELECT))
Serial.println("Select is being held");
if(ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed
Serial.print("Up held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
}
if(ps2x.Button(PSB_PAD_RIGHT)){
Serial.print("Right held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
}
if(ps2x.Button(PSB_PAD_LEFT)){
Serial.print("LEFT held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
}
if(ps2x.Button(PSB_PAD_DOWN)){
Serial.print("DOWN held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
}
vibrate = ps2x.Analog(PSAB_CROSS); //this will set the large motor vibrate speed based on how hard you press the blue (X) button
if (ps2x.NewButtonState()) { //will be TRUE if any button changes state (on to off, or off to on)
if(ps2x.Button(PSB_L3))
Serial.println("L3 pressed");
if(ps2x.Button(PSB_R3))
Serial.println("R3 pressed");
if(ps2x.Button(PSB_L2))
Serial.println("L2 pressed");
if(ps2x.Button(PSB_R2))
Serial.println("R2 pressed");
if(ps2x.Button(PSB_TRIANGLE))
Serial.println("Triangle pressed");
}
if(ps2x.ButtonPressed(PSB_CIRCLE)) //will be TRUE if button was JUST pressed
Serial.println("Circle just pressed");
if(ps2x.NewButtonState(PSB_CROSS)) //will be TRUE if button was JUST pressed OR released
Serial.println("X just changed");
if(ps2x.ButtonReleased(PSB_SQUARE)) //will be TRUE if button was JUST released
Serial.println("Square just released");
// if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { //print stick values if either is TRUE
Serial.println("Stick Values:");
Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
//Forward Direction And BAckward Diretion
if(ps2x.Button(PSB_R1)){
if(ps2x.Analog(PSS_RY)>=0)
{
//Forward Direction
if(ps2x.Analog(PSS_RY)<127)
{
Serial.println("All MOtor Running Forward");
digitalWrite(dir1,LOW);
digitalWrite(dir2,HIGH);
digitalWrite(dir3,HIGH);
digitalWrite(dir4,LOW);
value_mapped=map(ps2x.Analog(PSS_RY),127,0,0,80);
analogWrite(pwm1,value_mapped);
analogWrite(pwm2,value_mapped);
analogWrite(pwm3,value_mapped);
analogWrite(pwm4,value_mapped);
}
//BAckward Direction
if(ps2x.Analog(PSS_RY)>=127)
{
Serial.println("All MOtor Running Backward");
digitalWrite(dir1,HIGH);
digitalWrite(dir2,LOW);
digitalWrite(dir3,LOW);
digitalWrite(dir4,HIGH);
value_mapped=map(ps2x.Analog(PSS_RY),127,255,0,80);
analogWrite(pwm1,value_mapped);
analogWrite(pwm2,value_mapped);
analogWrite(pwm3,value_mapped);
analogWrite(pwm4,value_mapped);
}
}
// LEft OR Right Direction
if(ps2x.Analog(PSS_RX)>=0)
{
//Left diretion
if(ps2x.Analog(PSS_RX)<128)
{
Serial.println("All MOtor Running Left");
digitalWrite(dir1,HIGH);
digitalWrite(dir2,HIGH);
digitalWrite(dir3,LOW);
digitalWrite(dir4,LOW);
value_mapped=map(ps2x.Analog(PSS_RX),128,0,0,80);
analogWrite(pwm1,value_mapped);
analogWrite(pwm2,value_mapped);
analogWrite(pwm3,value_mapped);
analogWrite(pwm4,value_mapped);
}
//Right Direction
if(ps2x.Analog(PSS_RX)>128)
{
Serial.println("All MOtor Running Right");
digitalWrite(dir1,LOW);
digitalWrite(dir2,LOW);
digitalWrite(dir3,HIGH);
digitalWrite(dir4,HIGH);
value_mapped=map(ps2x.Analog(PSS_RX),128,255,0,80);
analogWrite(pwm1,value_mapped);
analogWrite(pwm2,value_mapped);
analogWrite(pwm3,value_mapped);
analogWrite(pwm4,value_mapped);
}
}
//All MOtors Clock
if( (ps2x.Analog(PSS_LX)<128) )
{
Serial.println("CLOCKWISE RUNNING ALL MOTORS");
digitalWrite(dir1,LOW);
digitalWrite(dir2,LOW);
digitalWrite(dir3,LOW);
digitalWrite(dir4,LOW);
value_mapped=map(ps2x.Analog(PSS_LX),128,0,0,80);
analogWrite(pwm1,value_mapped);
analogWrite(pwm2,value_mapped);
analogWrite(pwm3,value_mapped);
analogWrite(pwm4,value_mapped);
}
//All MOtors Anti-CLockwise
if(ps2x.Analog(PSS_LX)>128)
{
Serial.println("CLOCKWISE RUNNING ALL MOTORS");
digitalWrite(dir1,HIGH);
digitalWrite(dir2,HIGH);
digitalWrite(dir3,HIGH);
digitalWrite(dir4,HIGH);
value_mapped=map(ps2x.Analog(PSS_LX),128,255,0,80);
analogWrite(pwm1,value_mapped);
analogWrite(pwm2,value_mapped);
analogWrite(pwm3,value_mapped);
analogWrite(pwm4,value_mapped);
}
}
if(ps2x.Button(PSB_L1)){
if((ps2x.Analog(PSS_LX)>=120) && (ps2x.Analog(PSS_LX)<=128)){
analogWrite(pwm1,0);
analogWrite(pwm3,0);
}
//dIAGONAL L
if(ps2x.Analog(PSS_LX)<120)
{
Serial.println("CLOCKWISE RUNNING ALL MOTORS");
digitalWrite(dir1,LOW);
digitalWrite(dir3,HIGH);
value_mapped=map(ps2x.Analog(PSS_LX),128,0,0,80);
analogWrite(pwm1,value_mapped);
analogWrite(pwm3,value_mapped);
}
//dIAGONAL R
if(ps2x.Analog(PSS_LX)>=128)
{
Serial.println("CLOCKWISE RUNNING ALL MOTORS");
digitalWrite(dir2,HIGH);
digitalWrite(dir4,LOW);
value_mapped=map(ps2x.Analog(PSS_LX),128,255,0,80);
analogWrite(pwm2,value_mapped);
analogWrite(pwm4,value_mapped);
}
}
Serial.print(",");
Serial.print(ps2x.Analog(PSS_LX), DEC);
Serial.print(",");
Serial.print(ps2x.Analog(PSS_RY), DEC);
Serial.print(",");
Serial.println(ps2x.Analog(PSS_RX), DEC);
// }
}
delay(50);
}
Here's how I did it.....
void setup()
{
Serial.begin(9600);
// retry logic to avoid a reset after power up
do
{
delay(1000);
error = ps2x.config_gamepad(13, 11, 10, 12, true, true);
if (error == 1)
Serial.println("No controller found, retry in a sec");
} while (error == 1);
if (error == 0)
{
Serial.println("Found Controller, configured successful");
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
Serial.println("holding L1 or R1 will print out the analog stick values.");
Serial.println("Go to www.billporter.info for updates and to report bugs.");
}
else if (error == 2)
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
else if (error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
type = ps2x.readType();
switch (type)
{
case 0:
Serial.println("Unknown Controller type");

mbed not sleep with RTOS

I want to create a low power application with mbed (LPC1768) and have been following tutorial by Jim Hamblen at: https://mbed.org/cookbook/Power-Management and also http://mbed.org/users/no2chem/notebook/mbed-power-controlconsumption/
I was able to wake the mbed from Sleep() by GPIO interrupt, UART interrupt, and Ticker. I use PowerControl library.
Here is my code:
#include "mbed.h"
#include "PowerControl/PowerControl.h"
#include "PowerControl/EthernetPowerControl.h"
// Need PowerControl *.h files from this URL
// http://mbed.org/users/no2chem/notebook/mbed-power-controlconsumption/
// Function to power down magic USB interface chip with new firmware
#define USR_POWERDOWN (0x104)
int semihost_powerdown() {
uint32_t arg;
return __semihost(USR_POWERDOWN, &arg);
}
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);
bool rx_uart_irq = false;
Serial device(p28, p27); // tx, rx
InterruptIn button(p5);
// Circular buffers for serial TX and RX data - used by interrupt routines
const int buffer_size = 255;
// might need to increase buffer size for high baud rates
char tx_buffer[buffer_size];
char rx_buffer[buffer_size];
// Circular buffer pointers
// volatile makes read-modify-write atomic
volatile int tx_in=0;
volatile int tx_out=0;
volatile int rx_in=0;
volatile int rx_out=0;
// Line buffers for sprintf and sscanf
char tx_line[80];
char rx_line[80];
void Rx_interrupt();
void blink() {
myled2 = !myled2;
}
int main() {
//int result;
device.baud(9600);
device.attach(&Rx_interrupt, Serial::RxIrq);
// Normal mbed power level for this setup is around 690mW
// assuming 5V used on Vin pin
// If you don't need networking...
// Power down Ethernet interface - saves around 175mW
// Also need to unplug network cable - just a cable sucks power
PHY_PowerDown();
myled2 = 0;
// If you don't need the PC host USB interface....
// Power down magic USB interface chip - saves around 150mW
// Needs new firmware (URL below) and USB cable not connected
// http://mbed.org/users/simon/notebook/interface-powerdown/
// Supply power to mbed using Vin pin
//result = semihost_powerdown();
// Power consumption is now around half
// Turn off clock enables on unused I/O Peripherals (UARTs, Timers, PWM, SPI, CAN, I2C, A/D...)
// To save just a tiny bit more power - most are already off by default in this short code example
// See PowerControl.h for I/O device bit assignments
// Don't turn off GPIO - it is needed to blink the LEDs
Peripheral_PowerDown( ~( LPC1768_PCONP_PCUART0 |
LPC1768_PCONP_PCUART2 |
0));
// use Ticker interrupt and Sleep instead of a wait for time delay - saves up to 70mW
// Sleep halts and waits for an interrupt instead of executing instructions
// power is saved by not constantly fetching and decoding instructions
// Exact power level reduction depends on the amount of time spent in Sleep mode
//blinker.attach(&blink, 0.05);
//button.rise(&blink);
while (1) {
myled1 = 0;
printf("bye\n");
Sleep();
if(rx_uart_irq == true) {
printf("wake from uart irq\n");
}
myled1 = 1;
}
}
// Interupt Routine to read in data from serial port
void Rx_interrupt() {
myled2 = !myled2;
rx_uart_irq = true;
uint32_t IRR0= LPC_UART2->IIR;
while ((device.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
rx_buffer[rx_in] = LPC_UART2->RBR;
rx_in = (rx_in + 1) % buffer_size;
}
}
Here is the problem: The Sleep() doesn't put the mbed to sleep when mbed-rtos library is added. Even when I don't use any function calls from the rtos library , Sleep() doesn't work.
My explanation: Probably the rtos has a timer running in the background and it generates an interrupt every now and then. (But it kinda doesn't make sense because I haven't use any function or object from rtos library)
My question:
Has any one made the Sleep() function work with rtos? if yes, please point me to the right direction or if you have the solution, please share.
I'm not sure if the Sleep() function is designed for RTOS use, but I doubt it. Someone with better knowledge in mbed-rtos could probably tell for sure, but I suspect that IRQ handling in the RTOS could cause the problem. If Sleep() relies on WFE then the MCU will sleep if there is no pending interrupt flag. In a super loop design you (should) have full control over this; with an RTOS you don't.
I suggest using Thread::wait() instead, which should have full knowledge about what the RTOS does. Can't tell if it causes a sleep, but I expect no less.
I used the following library once and it worked flawlessly. I am not sure if it would work with mbed 5 but its worth a try.
https://os.mbed.com/users/no2chem/code/PowerControl/